SQL Declare Variable equivalent in databricks - azure-databricks

Can someone let me know if there is an DECLARE equivalent in Databricks SQL
The SQL Code that I have trying to execute with Databricks SQL is as follows:
DECLARE
#EnrichedViewDatabase sysname,
#EnrichedViewSchema sysname,
#EnrichedColumnSuffix varchar(50),
#LanguageCode varchar(10),
#BaseTableSuffix varchar(50),
#PreviewOnly bit, --Indicate whether to preview the SQL Script (without creating the views) = 1 ; Create views = 0;
#CurrentDatabase sysname,
#CurrentDatabaseSchema sysname
SET #EnrichedViewDatabase = 'mydatabasenr1'
SET #EnrichedViewSchema = 'dbo'
SET #EnrichedColumnSuffix = 'code'
SET #LanguageCode = 1033
SET #BaseTableSuffix = ''
SET #PreviewOnly = 0
SET #CurrentDatabase = 'mydatabasenr2'
SET #CurrentDatabaseSchema = 'dbo'
DECLARE #ColumnMetadata nvarchar(MAX), #ColumnMetadataSQL nvarchar(MAX)
The above SQL gives me the following error:
mismatched input 'DECLARE'
== SQL ==
DECLARE
^^^
#EnrichedViewDatabase sysname,
#EnrichedViewSchema sysname,
#EnrichedColumnSuffix varchar(50),
#LanguageCode varchar(10),
#BaseTableSuffix varchar(50),
#PreviewOnly bit, --Indicate whether to preview the SQL Script (without creating the views) = 1
Any thoughts?

DECLARE is not supported in Databricks SQL. The equivalent code to the one which you are trying to achieve in the above case would be to use SET directly.
%sql
SET EnrichedViewDatabase = 'mydatabasenr1';
SET EnrichedViewSchema = 'dbo';
SET EnrichedColumnSuffix = 'code';
SET LanguageCode = 1033;
SET BaseTableSuffix = '';
SET PreviewOnly = 0 ;
SET CurrentDatabase = 'mydatabasenr2';
SET CurrentDatabaseSchema = 'dbo';
SET returns a key-value pair as result. To access the value using the respective key (assigned in the above procedure), you can do it in the following way:
%sql
select ${hiveconf:CurrentDatabase} as x,${hiveconf:EnrichedViewDatabase} as y,${hiveconf:LanguageCode} as z

Related

Why I get error SQL command not properly ended while I execute UPDATE query

I am trying to migrate such a simple MySQL query to Oracle SQL using PLSQL
UPDATE submaterial SET `Name` = '{$name}'".($image !== null ? ", `Picture` = '{$image}'" : "")." WHERE SubmaterialID = {$id};
When I migrate to PLSQL I get something like this
UPDATE submaterial SET Name = p_name
CASE WHEN p_image != NULL THEN
Picture = p_image
WHERE SubmaterialID = p_Id;
When I want to execute this I get error
Error(408,6): PL/SQL: ORA-00933: SQL command not properly ended
I try also something like this but also get same error message
UPDATE submaterial SET Name = p_name
CASE WHEN p_image IS NOT NULL THEN
Picture = p_image
WHERE SubmaterialID = p_Id;
Does anyone know where did I made mistake ? How to write this king of query to Oracle SQL ?
That would be
UPDATE submaterial
SET Name = p_name,
picture = CASE WHEN p_image IS NOT NULL THEN p_image END
WHERE SubmaterialID = p_Id;
i.e. you have to take picture = out of case expression.

Azure Synapse Analysis: Error while accessing the external table

Failed to execute query. Error: File 'https://track2gen2storage.blob.core.windows.net/\sourcedata\sample.csv' cannot be opened because it does not exist or it is used by another process.
we performed these steps:-
create database SalesdataDemo
use salesdataDemo
-----create master key
CREATE MASTER KEY ENCRYPTION BY PASSWORD = <Password>;
SELECT *
FROM sys.symmetric_keys AS SK
WHERE SK.name = '##MS_DatabaseMasterKey##';
CREATE DATABASE SCOPED CREDENTIAL ADL_User
WITH
IDENTITY = '<client_id>#<OAuth_2.0_Token_EndPoint>',Secret = <Key>
CREATE DATABASE SCOPED CREDENTIAL adls_credential
WITH IDENTITY ='SHARED ACCESS SIGNATURE',
SECRET = <azure_storage_account_key>
CREATE EXTERNAL DATA SOURCE adlsdatasource
WITH
( LOCATION = 'https://track2gen2storage.blob.core.windows.net',
CREDENTIAL = adls_credential
) ;
CREATE EXTERNAL FILE FORMAT adls_csv
WITH (
FORMAT_TYPE = DELIMITEDTEXT,
FORMAT_OPTIONS ( FIELD_TERMINATOR = ',', STRING_DELIMITER = '"', FIRST_ROW = 2 )
);
CREATE EXTERNAL TABLE sampledata ( <ColumnName><Datatype>)
WITH (
LOCATION = '/sourcedata/sample.csv',
DATA_SOURCE = adlsdatasource,
FILE_FORMAT = adls_csv
)
select * from sampledata
I think the problem is your external table location is starting with /. Try changing it to:
CREATE EXTERNAL TABLE sampledata ( <ColumnName><Datatype>)
WITH (
LOCATION = 'sourcedata/sample.csv',
DATA_SOURCE = adlsdatasource,
FILE_FORMAT = adls_csv
)
Here is the document you can also take a look for reference:
https://learn.microsoft.com/en-us/azure/synapse-analytics/sql/create-use-external-tables
One more question, why do you need database scoped credential named ADL_User?

How to pass a dynamic list to ESQL Passthru query in IIB

I need to pass a dynamic list of parameters to SQL query in IIB. Please help me with any code snippets for this. Please note that we dont have privileges to create a Stored Procedure at DB end. So we need to pass the list of parameters to SQL query directly from IIB.
Scenario:
SET A= InputRoot.XMLNSC.Field1;
SET B= InputRoot.XMLNSC.Field2;
The number of above fields may vary dynamically.
SET query = select * from table where values in (?)
SET OutputRoot.XMLNSC.Result[] =Passthru (query To Database.DB) values (list)
Below is pseudocode. Assuming that you have a array of [LIST OF VALUES]. Try it out.
DECLARE PARM_1 CHAR '';
DECLARE QUERY CHAR;
FOR CURR AS {LIST OF VALUES} DO
SET PARM_1 = Write code to get list of values in the format => 'A','B','C'
END FOR;
--Comment: PARM_1 value now should be 'A','B','C'
--Comment : Below forming the query string and then use EVAL
SET QUERY = 'SELECT * FROM TABLE WHERE VALUE IN (' || PARM_1 || ')';
SET Environment.RESULT[] = EVAL(QUERY);

Bind variables in SELECT statement

I have tables in my Oracle database
PROJECTINFO
NAME SCHEMA
--------------------------
Test W_TEST_000
SAMPLESET
NAME SS_ID
--------------------------
Test_SSet 1049
in my SQL*Plus script a have a substitution variable (set directly or from user input)
DEFINE Project_Name = 'Test'
DEFINE SampleSet_Name = 'Test_SSet'
Now I need another two bind variables
VAR Project_Schema varchar2(50)
VAR SampleSet_ID number
Then I need to place a result of a SELECT statement into two bind vars
SELECT SCHEMA INTO :Project_Schema FROM PROJECTINFO WHERE NAME = '&Project_Name';
SELECT SS_ID INTO :SampleSet_ID from SAMPLESET WHERE NAME = '&SampleSet_Name';
Now I need to use both :Project_Schema and :SampleSet_ID in SELECT statement from W_TEST_000.MY_TABLE subtable like this:
SELECT NAME FROM :Project_Schema.MY_TABLE WHERE SS_ID = :SampleSet_ID
But this does not work.. (ORA-00903: invalid table name)
How to use bind variables in future SQL requests within the same SQL*Plus script?
it will work if you encapsulate the selects in PL/SQL blocks e.g.
DEFINE Project_Name = 'Test'
DEFINE SampleSet_Name = 'Test_SSet'
VAR Project_Schema varchar2(50)
VAR SampleSet_ID number
begin
SELECT SCHEMA
INTO :Project_Schema
FROM PROJECTINFO
WHERE NAME = '&Project_Name';
end;
/
begin
SELECT SS_ID
INTO :SampleSet_ID
from SAMPLESET
WHERE NAME = '&SampleSet_Name';
end;
/
--test the contents of the variable
Select :SampleSet_ID, :Project_Schema from dual;

sqlite, get field from updated

I have a an sqlite database with the table test. Several processes are accessing this database from bash. The table has the following fields:
CREATE TABLE mytable (id NUMERIC,
start JULIAN,
finish JULIAN)
I obtain an unique id by:
id=$(sqlite test.db <<EOF
BEGIN EXCLUSIVE;
SELECT id FROM mytable WHERE start IS NULL ORDER BY RANDOM() LIMIT 1;
COMMIT;
EOF
)
My question is, how can update the field start with:
UPDATE mytable set start=julianday('now') where id="SELECTED ID FROM ABOVE";
In the same statement?
Based on the comments that you supplied above, my solution would look something like follows (in perl with a raw DBI connection, also i didn't do a lot of error checking or anything either, something that you should probably do):
my $dbh = DBI->connect(...);
$dbh->do("BEGIN EXCLUSIVE");
my $stm = $dbh->prepare("SELECT id FROM mytable WHERE start IS NULL ORDER BY RANDOM() LIMIT 1");
$stm->execute();
my $row = $stm->fetchrow_hashref();
my $id = undef;
if ( $row ) {
$id = $row->{ID};
my $ustm = $dbh->prepare("UPDATE mytable set start=julianday('now') where id=?");
$ustm->execute($id);
}
$dbh->do("COMMIT");
# Still have the id at this point.

Resources