Getting below error while doing scd2 in snowflake using merge statement - snowflake-cloud-data-platform

My statement:
insert into target_scd2
select id,name,flag from (
merge into target_scd2 as t
using source as s
on t.id=s.id
when matched and t.name <> s.name then
update set flag='N'
when not matched then
insert values(s.id,s.name,'Y')
OUTPUT $Action action_flag,s.id,s.name,'Y'
) as merge_out
where merge_out.action_flag='UPDATE';
I am getting below error while executed the above statement:
SQL compilation error: syntax error line 3 at position 0 unexpected 'merge'. syntax error line 9 at position 29 unexpected ')'.
Can you please help what can be the issue

The pattern presented here is called: "INSERT over DML" and it is SQL Server specific.
More info: D. Inserting the results of the MERGE statement into another table
Can you please help what can be the issue
a) Using merge as subquery of INSERT SELECT
b) OUTPUT/(RETURNING) clause support
The Snowflake documentation of MERGE.

Related

Snowflake - Capture execution time of a query and insert it into a separate table

We have a tracking table used to monitor the runtime of processes. It captures data from other sources beyond Snowflake so we can't just use the history data that is automatically captured when queries run. Our idea is to have a pre statement that captures the start time as a variable, a snowflake query would then run (could be anything), and lastly a post statement which outputs the required tracking data into a different table. However, when we try the below statement or variations of it we get an error.
Error:
SQL compilation error: syntax error line 3 at position 0 unexpected 'SELECT'. syntax error line 8 at position 0 unexpected 'INSERT'.
Statement Example:
-- Pre Statement: setting the start time of the query
SET(starttime)=current_timestamp()
-- Intra Statement: query to run in Snowflake
SELECT *
FROM "Customer"
-- Post Statement: inserting output into tracking table
INSERT INTO "Tracking"
SELECT 'Example', current_timestamp(), current_warehouse(), DATEDIFF(hour,$starttime,current_timestamp()), current_user();
Tracking Table Fields:
PROCESS_TAG, RUN_DATE, WAREHOUSE, RUN_TIME, ACCOUNT
Thank you #Sergiu and #Greg Pavlik for finding my mistake of not including ";" after each statement. The query runs as expected now.
-- Pre Statement: setting the start time of the query
SET(starttime)=current_timestamp();
-- Intra Statement: query to run in Snowflake
SELECT *
FROM "Customer";
-- Post Statement: inserting output into tracking table
INSERT INTO "Tracking"
SELECT 'Example', current_timestamp(), current_warehouse(), DATEDIFF(hour,$starttime,current_timestamp()), current_user();

BigQuery Execute Immediate Identifier issue

I am creating a BQ Stored Procedure to truncate all the tables in a dataset. I have a 2 step process. Step 1 identifies all the matching tables. Step 2 is expected to iterate thru each table and truncate.
I have the following code to achieve this:
for record in
( select TABLE_NAME
from <staging_dataset>.INFORMATION_SCHEMA.TABLES
)
DO
execute immediate
"truncate table #tab" using record.TABLE_NAME as tab;
END FOR;
The Error that I am running into is in the Execute Immediate piece.
Invalid EXECUTE IMMEDIATE sql string `truncate table #tab`, Syntax error: Unexpected "#" at [8:3]
I tried replacing the #tab with ? placeholder and see a similar error. What am I doing wrong?
Is there another way to achieve the same result?
Strange; it seems DML doesn't work with USING. Queries work fine.
Try using CONCAT to build your dynamic query string:
CONCAT("truncate table ", record.TABLE_NAME);

“union all” two data set with different column names in sas using Hadoop connection

I am trying to append two datasets in a proc sql query. I am using Hadoop connection for this job. One data has one extra column which I am using to subset the data. Code patch:
proc sql;
Create table t1 as select * from connection to Hadoop
(select temp.* from
(select a.* , “ ” as var1 from table1(keep= &keeplist) a
union all
select b.* from table2(keep= &keeplist var1) b where b.var1 = “XX”) temp);
quit;
This code gives the following error:
ERROR: Prepare error: Error while compiling statement: FAILED: ParseException
line 1:67 mismatched input ‘(‘ expecting ) near ‘table1’ in sub query source
Previously I have also tried excluding the sub query and that resulted in error saying : missing EOF at ‘(‘ near ‘table1’.
Any know solution to this sort of query? otherwise I am thinking to pull separately and then append, that might incur higher cost.

Insert Syntax in SQL: Without INTO and VALUE

I'm understanding a stored procedure, here's part of the code:
INSERT [dbo].[PartitionMaintenanceTables] (
nvc_TableSchema,
nvc_TableName,
i_CompressInterval,
vc_CompressType,
i_RetainInterval,
dt_CreatedDatetime,
dt_ChangedDatetime,
dt_DeletedDatetime,
ti_NeedsRepl,
nvc_ChangedDatabaseName
)
As you can see, it's not inserting any value into this table. What does it mean? Is it inserting a bunches of default / null values to them?
Syntax like:
INSERT tab(col); -- is invalid
INTO is optional. As for missing VALUES clause you probably have SELECT after insert:
INSERT tab(col) SELECT ...;
db<>fiddle demo
That code by itself will generate a syntax error:
Incorrect syntax near ')'.

SQL MERGE: An expression of non-boolean type specified in a context where a condition is expected, near ','

merge into emp_tar.et
using emp_src.es on (et.emp_id, es.emp_id)
insert into (et.emp_id,et.emp_name)
values(es.emp_id,es.emp_name)
Whenever I execute this code it gives the error:
An expression of non-boolean type specified in a context where a
condition is expected, near ','
Here is the right Merge syntax
Merge into emp_tar et
using emp_src es on et.emp_id = es.emp_id -- use = to equate two columns
When Not Matched then -- to insert the records only it is not present in target
insert into (et.emp_id,et.emp_name)
values(es.emp_id,es.emp_name)
Which is same as
Insert into emp_tar(emp_id,emp_name)
Select es.emp_id,es.emp_name
From emp_src es
Where Not exists (select 1 from emp_tar et Where et.emp_id = es.emp_id)
Merge statement can be used when you want to perform more than one DML operation. Also Merge in Sql Server is quite buggy better to use Insert from Select in your case
Use Caution with SQL Server's MERGE Statement

Resources