Translation reference to convert SQL Server SET statement to Snowflake
Description
Sets the specified local variable, previously created by using the DECLARE @local_variable statement, to the specified value. For more information for SQL Server SET, check here.
CREATEORALTERPROCEDURE SetProcedureASDECLARE @MyCounter INT; DECLARE @FloatCounter FLOAT; --Numerical operatorsSET @MyCounter =3; SET @MyCounter +=1; --@MyCounter has 4SET @MyCounter -=1; --@MyCounter has 3SET @MyCounter *=2; --@MyCounter has 6SET @MyCounter /=3; --@MyCounter has 2SET @MyCounter =6; SET @MyCounter /=5; --@MyCounter has 1SET @MyCounter =6; SET @MyCounter /=7; --@MyCounter has 0SET @FloatCounter =10;SET @FloatCounter /=4; --@FloatCounter has 2.5SET @MyCounter =6; SET @MyCounter %=4; --@MyCounter has 2--Logical operatorsSET @MyCounter &=3; --@MyCounter has 2SET @MyCounter ^=2; --@MyCounter has 0SET @MyCounter |=0; --@MyCounter has 0RETURN @MyCounter;GODECLARE @result INT;EXEC @result = SetProcedure;PRINT @result;
IN -> SqlServer_02.sql
CREATETABLEvEmployee ( PersonID int, LastName varchar(255), FirstName varchar(255));CREATEORALTERPROCEDURE SetCursorASDECLARE @CursorVar CURSOR; SET @CursorVar =CURSORSCROLLDYNAMICFORSELECT LastName, FirstName FROM vEmployee WHERE LastName like'B%'; GO
|Result |
|---------|
|0 |
Snowflake Scripting
OUT -> SqlServer_01.sql
CREATEORREPLACEPROCEDURE SetProcedure ()RETURNSVARCHARLANGUAGESQLCOMMENT ='{"origin":"sf_sc","name":"snowconvert","version":{"major":1, "minor":0},"attributes":{"component":"transact"}}'EXECUTEASCALLERAS$$DECLARE MYCOUNTER INT; FLOATCOUNTER FLOAT;BEGIN--Numerical operators MYCOUNTER :=3; MYCOUNTER := MYCOUNTER +1; --@MyCounter has 4 MYCOUNTER := MYCOUNTER -1; --@MyCounter has 3 MYCOUNTER := MYCOUNTER *2; --@MyCounter has 6 MYCOUNTER := TRUNC(MYCOUNTER /3); --@MyCounter has 2 MYCOUNTER :=6; MYCOUNTER := TRUNC(MYCOUNTER /5); --@MyCounter has 1 MYCOUNTER :=6; MYCOUNTER := TRUNC(MYCOUNTER /7); --@MyCounter has 0 FLOATCOUNTER :=10; FLOATCOUNTER := FLOATCOUNTER /4; --@FloatCounter has 2.5 MYCOUNTER :=6; MYCOUNTER := MYCOUNTER % 4; --@MyCounter has 2--Logical operators MYCOUNTER := BITAND(MYCOUNTER, 3); --@MyCounter has 2 MYCOUNTER := BITXOR(MYCOUNTER, 2); --@MyCounter has 0 MYCOUNTER := BITOR(MYCOUNTER, 0); --@MyCounter has 0RETURN :MYCOUNTER;END;$$;DECLARE RESULT INT;BEGINCALL SetProcedure(); !!!RESOLVE EWI!!! /*** SSC-EWI-0073 - PENDING FUNCTIONAL EQUIVALENCE REVIEW FOR 'Print' NODE ***/!!!PRINT @result;END;
OUT -> SqlServer_02.sql
CREATE OR REPLACETABLEvEmployee ( PersonID INT, LastName VARCHAR(255), FirstName VARCHAR(255))COMMENT ='{"origin":"sf_sc","name":"snowconvert","version":{"major":1, "minor":0},"attributes":{"component":"transact"}}';CREATEORREPLACEPROCEDURE SetCursor ()RETURNSVARCHARLANGUAGESQLCOMMENT ='{"origin":"sf_sc","name":"snowconvert","version":{"major":1, "minor":0},"attributes":{"component":"transact"}}'EXECUTEASCALLERAS$$DECLARE !!!RESOLVE EWI!!! /*** SSC-EWI-TS0037 - SNOWFLAKE SCRIPTING CURSORS ARE NON-SCROLLABLE, ONLY FETCH NEXT IS SUPPORTED ***/!!!--** SSC-FDM-TS0013 - SNOWFLAKE SCRIPTING CURSOR ROWS ARE NOT MODIFIABLE ** CURSORVAR CURSORFORSELECT LastName, FirstNameFROM vEmployeeWHERE LastName like'B%';BEGINRETURN'';END;$$;
|Result |
|---------|
|0 |
SET statement outside routines (functions and procedures)
Unlike SQL Server, Snowflake does not support executing isolated statements like SET outside routines like functions or procedures. For this scenario, the statement should be encapsulated in an anonymous block, as shown in the following examples. This statement is usually used after a DECLARE STATEMENT.
If there is a scenario with only SET statements, the DECLARE block is not necessary. Probably this scenario will produce runtime errors if there is an attempt of setting a value to a variable that is not declared.
SQL Server
IN -> SqlServer_04.sql
SET @Group =N'North America';
Snowflake Scripting
OUT -> SqlServer_04.sql
BEGIN_GROUP :='North America';END;
Known Issues
1. SET of a local variable with property name
This type of set is not currently supported by Snowflake scripting.
//TSQL custom datatypewith properties example DECLARE @p Point; SET @p.X = @p.X +1.1;
2. SET of a local variable with mutator method
This type of set is not currently supported by Snowflake scripting.