SnowConvert AI - SQL Server-Azure Synapse - CREATE PROCEDURE (Snowflake Scripting)¶
BEGIN and COMMIT Transaction¶
Translation reference to convert Transact-SQL BEGIN and COMMIT transaction to Snowflake SQL
Applies to
SQL Server
Azure Synapse Analytics
Description¶
Snowflake SQL, a transaction can be started explicitly by executing a BEGIN statement. Snowflake supports the synonyms BEGIN WORK and BEGIN TRANSACTION. Snowflake recommends using BEGIN TRANSACTION.
A transaction can be ended explicitly by executing COMMIT. For more information, see the Snowflake Transactions documentation.
Sample Source Patterns¶
The following examples detail the BEGIN and COMMIT transaction statements.
Transact-SQL¶
BEGIN/COMMIT TRANSACTION¶
Begin/Commit transaction with label¶
Snowflake SQL¶
BEGIN/COMMIT¶
BEGIN/COMMIT transaction with label¶
Known Issues¶
Nested transactions are not supported in Snowflake. Review the following documentation for more information: https://docs.snowflake.com/en/sql-reference/transactions
CALL¶
Translation reference for CALL statement
Applies to
SQL Server
Azure Synapse Analytics
Description¶
The CALL statement is not supported in Snowflake Scripting since this is part of the ODBC API and not a SQL statement, therefore this statement is not translated.
CASE¶
Translation reference to convert Transact-SQL Case expression to Snowflake Scripting
Applies to
SQL Server
Azure Synapse Analytics
Note
Some parts in the output code are omitted for clarity reasons.
Description¶
Transact-SQL has two possible formats of the Case expression. both of them for the purpose of evaluating expressions and conditionally obtaining results. The first one refers to a Simple Case Expression that will evaluate if an input_expression matches one or more of the when_expression. The second one will evaluate each Boolean_expression independently. The else clause is supported in both formats.
According to the official Transact-SQL Case documentation:
CASE can be used in any statement or clause that allows a valid expression. For example, you can use CASE in statements such as SELECT, UPDATE, DELETE and SET, and in clauses such as select_list, IN, WHERE, ORDER BY, and HAVING.
For more information, see the Transact-SQL CASE documentation.
Note: Transact-SQL allows to optionally encapsulate the input_expression and the boolean_expression in parentheses; Snowflake Scripting too.
Sample Source Patterns¶
The following examples detail two scenarios where the Case expression can be used and their differences from Snowflake Scripting.
Select using Case¶
Transact-SQL¶
Simple CASE¶
Searched CASE¶
Result¶
sqlLOGINID |
status |
|---|---|
adventure-works\ken0 |
SINGLE |
adventure-works\terri0 |
SINGLE |
adventure-works\roberto0 |
MARIED |
adventure-works\rob0 |
SINGLE |
adventure-works\gail0 |
MARIED |
adventure-works\jossef0 |
MARIED |
adventure-works\dylan0 |
MARIED |
adventure-works\diane1 |
SINGLE |
adventure-works\gigi0 |
MARIED |
adventure-works\michael6 |
MARIED |
Snowflake Scripting¶
Note that in this scenario there are no differences regarding the Case expression itself.
Warning
The declaration and assignment of the res variable is to demonstrate the functional equivalence between both languages. It does not appear in the actual output.
Simple CASE¶
Searched CASE¶
Result¶
LOGINID |
STATUS |
|---|---|
adventure-worksken0 |
SINGLE |
adventure-works erri0 |
SINGLE |
adventure-worksoberto0 |
MARIED |
adventure-worksob0 |
SINGLE |
adventure-worksgail0 |
MARIED |
adventure-worksjossef0 |
MARIED |
adventure-worksdylan0 |
MARIED |
adventure-worksdiane1 |
SINGLE |
adventure-worksgigi0 |
MARIED |
adventure-worksmichael6 |
MARIED |
Set using Case¶
The AdventureWorks2019 database was used in both languages to obtain the same results.
Transact-SQL¶
Simple Case¶
Searched Case¶
Result¶
result |
|---|
150 |
Snowflake Scripting¶
Warning
Snowflake Scripting does not allow setting a case expression directly to a variable. Both Transact-SQL Case expression formats translate to the following grammar in Snowflake Scripting.
SimpleCase¶
Searched Case¶
Result¶
result |
|---|
150 |
Related EWIs¶
SSC-EWI-0073: Pending Functional Equivalence Review.
CREATE PROCEDURE¶
Translation reference to convert Transact-SQL CREATE PROCEDURE clauses to Snowflake
Applies to
SQL Server
Azure Synapse Analytics
Note
Some parts in the output code are omitted for clarity reasons.
Description¶
The create procedure statement allows the creation of stored procedures that can:
Accept input parameters and return multiple values in the form of output parameters to the calling procedure or batch.
Contain programming statements that perform operations in the database, including calling other procedures.
Return a status value to a calling procedure or batch to indicate success or failure (and the reason for failure).
For more information, see the Transact-SQL CREATE PROCEDURE documentation.
Sample Source Patterns¶
Stored procedure without body¶
A stored procedure without a body is an unusual scenario that is allowed in Transact-SQL. Snowflake Scripting does not allow defining procedures without a body, but the following example shows the equivalence.
Transact-SQL¶
Procedure¶
Snowflake Scripting¶
Basic stored procedure¶
The following example details a simple stored procedure that will include a new Privacy department into the AdventureWorks2019 database.
Transact-SQL¶
Snowflake Scripting¶
Alter procedure¶
The transformation for the ALTER procedure is equivalent to the basic procedure.
Transact-SQL¶
Snowflake Scripting¶
Using parameters¶
You can use parameters to drive your logic or construct dynamic SQL statements inside your stored procedure. In the following example a simple SetNewPrice stored procedure is constructed, which sets a new product price based on the arguments sent by the caller.
Transact-SQL¶
Snowflake Scripting¶
Output Parameters¶
Transact-SQL output keyword indicates that the parameter is an output parameter, whose value will be returned to the stored procedure caller. For example, the following procedure will return the number of vacation hours of a specific employee.
Transact-SQL¶
Snowflake Scripting¶
Optional Parameters¶
A parameter is considered optional if the parameter has a default value specified when it is declared. It is not necessary to provide a value for an optional parameter in a procedure call.
Transact-SQL¶
Snowflake Scripting¶
EXECUTE AS¶
Transact-SQL’s EXECUTE AS clause defines the execution context of the stored procedure, specifying which user account the Database Engine uses to validate permissions on objects that are referenced within the procedure. For example, we can modify the previous GetVacationHours procedure to define different execution contexts.
Owner (default in Snowflake Scripting)
Transact-SQL¶
Snowflake Scripting¶
Caller¶
Transact-SQL¶
Snowflake Scripting¶
Warning
SELF and specific user (‘user_name’) execution contexts are not supported in Snowflake Scripting.
READONLY AND VARYING PARAMETERS¶
Snowflake does not support READONLY and VARYING parameter types, an FDM is added instead.
Transact-SQL¶
Snowflake Scripting¶
Known Issues¶
Unsupported Optional Arguments¶
[VARYING] Applies only to cursor parameters.Specifies the result set supported as an output parameter. This parameter is dynamically constructed by the procedure and its contents may vary. Snowflake scripting does not support CURSOR as a valid return data type.
[= default] Makes a parameter optional through the definition of a default value. Snowflake scripting does not natively supports default parameter values.
[READONLY] Indicates that the parameter cannot be updated or modified within the body of the procedure. Currently unsupported in Snowflake Scripting.
[WITH RECOMPILE] Forces the database engine to compile the stored procedure’s query plan each time it is executed. Currently unsupported in Snowflake Scripting.
[WITH ENCRYPTION] Used to encrypt the text of a stored procedure. Only users with access to system tables or database files (such as sysadmin users) will be able to access the procedure text after its creation. Currently unsupported in Snowflake Scripting.
[FOR REPLICATION] Restricts the stored procedure to be executed only during replication. Currently unsupported in Snowflake Scripting.
Related EWIs¶
SSC-EWI-0030: The statement below has usages of dynamic SQL.
SSC-EWI-0058: Functionality is not currently supported by Snowflake Scripting.
CURSOR¶
Translation reference to convert Transact-SQL CURSOR statement to Snowflake Scripting
Applies to
SQL Server
Azure Synapse Analytics
Note
Some parts in the output code are omitted for clarity reasons.
Description¶
Transact-SQL statements produce a complete result set, but there are times when the results are best processed one row at a time. Opening a cursor on a result set allows processing the result set one row at a time. You can assign a cursor to a variable or parameter with a cursor data type. For more information, see the Transact-SQL Cursors documentation.
Sample Source Patterns¶
Transact-SQL¶
Notice that the following parameters are inherently supported by Snowflake Scripting.
[LOCAL].
[FORWARD_ONLY].
[FAST_FORWARD] Specifies a FORWARD_ONLY (FETCH NEXT only) and READ_ONLY
[READ_ONLY] the WHERE CURRENT OF does not exist in Snowflake Scripting.
Cursor¶
Result¶
Snowflake Scripting¶
Cursor¶
Result¶
Known Issues¶
The following parameters are not supported:
DECLARE CURSOR
[ GLOBAL ] Allows referencing the cursor name in any stored procedure or batch executed by the connection. Snowflake Scripting only allows the use of the cursor locally.
[ SCROLL ] Snowflake Scripting only support FETCH NEXT.
[ KEYSET | DYNAMIC ] If after opening a cursor and update to the table is made, these options may display some of the changes when fetching the cursor, Snowflake scripting only supports STATIC, in other words, after the cursor is opened the changes to the table are not detected by the cursor.
[SCROLL_LOCKS] Specifies that positioned updates or deletes made through the cursor are guaranteed to succeed, Snowflake Scripting cannot guarantee it.
[OPTIMISTIC] When an update or delete is made through the cursor it uses comparisons of timestamp column values, or a checksum value if the table has no timestamp column, to determine whether the row was modified after it was read into the cursor. Snowflake Scripting does not have an internal process to replicate it.
[TYPE_WARNING]
FETCH
[PRIOR | FIRST | LAST] Snowscripting only support NEXT.
[ABSOLUTE] Snowflake Scripting only supports NEXT but the behavior can be replicated.
[RELATIVE] Snowflake Scripting but the behavior can be replicated.
[ GLOBAL ] Allows referencing the cursor name in any stored procedure or batch executed by the connection. Snowflake Scripting only allows the use of the cursor locally.
FETCH without INTO is not supported.
When the FETCH statement is located inside a loop it is considered a complex pattern as it may have an impact on the Snowflake translated code performance. Check the related issues section for more information.
Fetch inside loop sample¶
SQL Server¶
Snowflake¶
OPEN¶
[ GLOBAL ] Allows referencing the cursor name in any stored procedure or batch executed by the connection. Snowflake Scripting only allows the use of the cursor locally.
CLOSE
[ GLOBAL ] Allows referencing the cursor name in any stored procedure or batch executed by the connection. Snowflake Scripting only allows the use of the cursor locally.
DEALLOCATED Removes a cursor reference and there is no equivalent in Snowflake Scripting.
WHERE CURRENT OF the use of this statement is not supported, for example:
Environment variables
@@CURSOR_ROWS
@@FETCH_STATUS
Related EWIs¶
SSC-FDM-TS0013: Snowflake Scripting cursor rows are not modifiable.
SSC-PRF-0003: Fetch inside a loop is considered a complex pattern, this could degrade Snowflake performance.
DECLARE¶
Translation reference to convert Transact-SQL DECLARE statement to Snowflake Scripting
Applies to
SQL Server
Azure Synapse Analytics
Description¶
Transact-SQL DECLARE statement allows the creation of variables that can be used in the scope of the batch or a stored procedure. For more information, see the Transact-SQL DECLARE documentation.
Sample Source Patterns¶
Declare variables¶
Variables can be created in different ways. Variables may or may not have a default value and several variables can be declared in the same line.
Notice that Snowflake Scripting does not allow creating more than one variable per line.
Transact-SQL¶
Snowflake Scripting¶
Declare table variables¶
Transact-SQL allows the creation of table variables that can be used as regular tables. Snowflake scripting does not support this, instead, a table can be created and then dropped at the end of the procedure.
Transact-SQL¶
Snowflake Scripting¶
DECLARE statement outside routines (functions and procedures)¶
Unlike Transact-SQL, Snowflake does not support executing isolated statements like DECLARE 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 before a SET STATEMENT.
Transact-SQL¶
Snowflake Scripting¶
If there is a scenario with only DECLARE statements, the BEGIN…END block should have a RETURN NULL statement to avoid errors, since this block can’t be empty.
Transact-SQL¶
Snowflake Scripting¶
EXECUTE¶
Translation reference to convert Transact-SQL Execute statement to Snowflake
Applies to
SQL Server
Azure Synapse Analytics
Note
Some parts in the output code are omitted for clarity reasons.
Description¶
Transact-SQL EXECUTE statement allows the execution of a command string or character string within a Transact-SQL batch, a scalar-valued user-defined function, or a stored procedure. For more information, see the Transact-SQL EXECUTE documentation.
Sample Source Patterns¶
Execution of character string¶
EXECUTE can be used to perform SQL operations passed directly as literals. In the following example it is used within a stored procedure that will insert a new privacy department into the AdventureWorks2019 database.
Transact-SQL¶
Snowflake Scripting¶
Execution of stored procedure¶
EXECUTE can also be used to call an existing stored procedure. The following example will call the AddPrivacyDepartment procedure that was created above. It will then run a SELECT to verify that the new department was successfully included.
Transact-SQL¶
Result¶
DepartmentID |
Name |
GroupName |
ModifiedDate |
|---|---|---|---|
1 |
Engineering |
Research and Development |
2008-04-30 00:00:00.000 |
2 |
Tool Design |
Research and Development |
2008-04-30 00:00:00.000 |
3 |
Sales |
Sales and Marketing |
2008-04-30 00:00:00.000 |
4 |
Marketing |
Sales and Marketing |
2008-04-30 00:00:00.000 |
5 |
Purchasing |
Inventory Management |
2008-04-30 00:00:00.000 |
6 |
Research and Development |
Research and Development |
2008-04-30 00:00:00.000 |
7 |
Production |
Manufacturing |
2008-04-30 00:00:00.000 |
8 |
Production Control |
Manufacturing |
2008-04-30 00:00:00.000 |
9 |
Human Resources |
Executive General and Administration |
2008-04-30 00:00:00.000 |
1 0 |
Finance |
Executive General and Administration |
2008-04-30 00:00:00.000 |
1 1 |
Information Services |
Executive General and Administration |
2008-04-30 00:00:00.000 |
1 2 |
Document Control |
Quality Assurance |
2008-04-30 00:00:00.000 |
1 3 |
Quality Assurance |
Quality Assurance |
2008-04-30 00:00:00.000 |
1 4 |
Facilities and Maintenance |
Executive General and Administration |
2008-04-30 00:00:00.000 |
1 5 |
Shipping and Receiving |
Inventory Management |
2008-04-30 00:00:00.000 |
1 6 |
Executive |
Executive General and Administration |
2008-04-30 00:00:00.000 |
1 7 |
Privacy |
Executive General and Administration |
2021-11-17 12:42:54.640 |
Snowflake Scripting¶
Result¶
DEPARTMENTID |
NAME |
GROUPNAME |
MODIFIEDDATE |
|---|---|---|---|
1 |
Engineering |
Research and Development |
2021-11-17 10:29:36.963 |
2 |
Tool Design |
Research and Development |
2021-11-17 10:29:37.463 |
3 |
Sales |
Sales and Marketing |
2021-11-17 10:29:38.192 |
4 |
Marketing |
Sales and Marketing |
2021-11-17 10:29:38.733 |
5 |
Purchasing |
Inventory Management |
2021-11-17 10:29:39.298 |
6 |
Research and Development |
Research and Development |
2021-11-17 10:31:53.770 |
7 |
Production |
Manufacturing |
2021-11-17 10:31:55.082 |
8 |
Production Control |
Manufacturing |
2021-11-17 10:31:56.638 |
9 |
Human Resources |
Executive General and Administration |
2021-11-17 10:31:57.507 |
10 |
Finance |
Executive General and Administration |
2021-11-17 10:31:58.473 |
11 |
Information Services |
Executive General and Administration |
2021-11-17 10:34:35.200 |
12 |
Document Control |
Quality Assurance |
2021-11-17 10:34:35.741 |
13 |
Quality Assurance |
Quality Assurance |
2021-11-17 10:34:36.277 |
14 |
Facilities and Maintenance |
Executive General and Administration |
2021-11-17 10:34:36.832 |
15 |
Shipping and Receiving |
Inventory Management |
2021-11-17 10:34:37.373 |
16 |
Executive |
Executive General and Administration |
2021-11-17 10:34:37.918 |
17 |
Privacy |
Executive General and Administration |
2021-11-17 10:46:43.345 |
Execution of local variable and use of parameters¶
A common use case for the EXECUTE statement is when dynamic SQL statements are needed. In this cases instead of executing a string literal, the statement could be constructed dynamically and assigned to a local variable, which will then be executed. A set of arguments can be sent to the called stored procedure to construct the dynamic SQL command.
In the following example a simple SetNewPrice stored procedure is constructed, which uses the EXECUTE statement to set a new product price based on the arguments sent by the caller. Lastly a SELECT is performed to confirm the new product price.
Transact-SQL¶
Result¶
ListPrice |
|---|
34.9900 |
Snowflake Scripting¶
Result¶
LISTPRICE |
|---|
34.9900 |
Known Issues¶
Using return codes¶
Transact-SQL EXECUTE syntax contains the @return_status optional argument, which allows creating a scalar variable to store the return status of a scalar-valued user defined function.
It can also be used in stored procedures although the returning status will be limited to integer data type.
To represent this functionality, we could slightly modify the above example and create a user defined function to calculate the new product price as an average of the historical prices. Instead of passing it to the stored procedure, we could now call the CalculateAveragePrice function to obtain the new price, and store it in the return variable to construct the dynamic SQL.
Transact-SQL¶
Execute¶
Result¶
ListPrice |
|---|
34.0928 |
Snowflake Scripting¶
Unsupported Optional arguments¶
@return_status
;number
@module__name_v_ar
WITH RECOMPILE, WITH RESULT SETS NONE, WITH <result set definition>
Related EWIs¶
SSC-EWI-0030: The statement below has usages of dynamic SQL.
IF¶
Translation reference to convert Transact-SQL IF..ELSE clauses to Snowflake Scripting
Applies to
SQL Server
Azure Synapse Analytics
Description¶
The IF clause allows an SQL statement or a block of statements to be conditionally executed as long as the Boolean expression is true; otherwise, the statements in the optional ELSE clause will be executed. Transact-SQL also supports embedding multiple IF… ELSE clauses in case multiple conditions are required, or the CASE clause can also be used.
For more information, see the Transact-SQL IF…ELSE documentation.
Note: To define a statement block, use the control-of-flow keywords BEGIN and END.
Sample Source Patterns¶
Transact-SQL¶
The following code refers to an IF… ELSE in Transact-SQL that conditions the variable @value to identify if it is less than 5, if it is between 5 and 10, or if it has any other value. Since @value is initialized as 7, the second condition must be true and the result must be 200.
IF…ELSE¶
Result¶
result |
|---|
200 |
Snowflake Scripting¶
Note
Notice that in Snowflake Scripting, the embedded IF… ELSE condition is called ELSEIF.
Besides, the Boolean condition is encapsulated in parentheses and the clause always ends with the END IF expression.
In addition, in Snowflake Scripting it is not necessary to use the BEGIN and END keywords to define a statement block, however it can be used if required.
IF…ELSE¶
Result¶
result |
|---|
200 |
IF statement outside routines (functions and procedures)¶
Unlike Transact-SQL, Snowflake does not support executing isolated statements like IF…ELSE outside routines like functions or procedures. For this scenario, the statement should be encapsulated in an anonymous block, as shown in the following example. You can read more about how to correctly return the output values in the SELECT section.
Transact-SQL¶
Snowflake Scripting¶
Related EWIs¶
SSC-EWI-0073: Pending Functional Equivalence Review.
SSC-FDM-0020: Multiple result sets are returned in temporary tables.
LABEL and GOTO¶
Translation reference to convert LABEL AND GOTO in Transact-SQL
Applies to
SQL Server
Description¶
Snowflake SQL does not support GOTO LABEL statements. Currently, LABELS are commented and warning is added for all the occurrences.
Sample Source Patterns¶
The following examples details the BEGIN and COMMIT transaction statements.
Transact-SQL¶
Labeled statements¶
Snowflake SQL¶
Labeled statements¶
LABEL and GOTO statement outside routines (functions and procedures)¶
Transact-SQL¶
Snowflake Scripting¶
Related EWIs¶
SSC-EWI-TS0045: Labeled Statement is not supported in Snowflake Scripting.
SSC-EWI-0073: Pending Functional Equivalence Review.
OUTPUT PARAMETERS¶
This article is about the current transformation of the output parameters and how their functionality is being emulated.
Applies to
SQL Server
Azure Synapse Analytics
Note
Some parts in the output code are omitted for clarity reasons.
Description¶
An output parameter is a parameter whose value is passed out of the stored procedure, back to the calling SQL block. Since the output parameters are not supported by Snowflake Scripting, a solution has been implemented to emulate their functionality.
Sample Source Patterns¶
Single OUT parameter¶
The most basic scenario for OUT parameters is when the procedure only has one. In this case, we simply return the OUT parameter at the end of the procedure body.
The EXEC procedure has to be translated as well, for this a CALL is created, the parameters are passed without any modifier (“OUT” is removed), and subsequently, an assignment is done so the parameter is associated with it’s respective resulting value.
Transact-SQL¶
Snowflake Scripting¶
Multiple OUT parameters¶
When more than one OUT parameters are found, the RETURNS clause of the procedure changes to VARIANT. This is to accommodate the OBJECT_CONSTRUCT that is going to be used to store the values of the OUT parameters.
On top of that, a RETURN statement is added to the end of the procedure’s body. This is where the OBJECT_COSNTRUCT is created and all the OUT parameter values are stored within it. This object will then be used by the caller to assign the parameters value to the corresponding result.
Transact-SQL¶
Snowflake Scripting¶
OUT parameters and return values¶
Transact-SQL allows procedures to have return values. When a procedure has both a return value and OUT parameter(s), a similar approach to the Multiple OUT parameters scenario is followed. The original return value is treated as an OUT parameter would be treated, so it’s stored within the OBJECT_CONSTRUCT and extracted inside the caller procedure.
Transact-SQL¶
Snowflake Scripting¶
Query¶
Customer data type OUT parameters¶
when the output parameter is a custom type, the process is similar to a regular data type.
Transact-SQL¶
Snowflake Scripting¶
Known Issues¶
No issues were found.
Related EWIs¶
SSC-EWI-0073: Pending Functional Equivalence Review.
SSC-EWI-TS0015: Data type is not supported in Snowflake.
SET¶
Translation reference to convert Transact-SQL SET statement to Snowflake
Applies to
SQL Server
Azure Synapse Analytics
Description¶
Sets the specified local variable, previously created by using the DECLARE @local_variable statement, to the specified value. For more information, see the Transact-SQL SET documentation.
There are four SET cases that are the following:
Sample Source Patterns¶
Transact-SQL¶
Case 1¶
Case 2¶
Result 1¶
Result |
|---|
0 |
Snowflake Scripting¶
Case 1¶
Case 2¶
Result 1¶
Result |
|---|
0 |
SET statement outside routines (functions and procedures)¶
Unlike Transact-SQL, 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.
Transact-SQL¶
Snowflake Scripting¶
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.
Transact-SQL¶
Snowflake Scripting¶
Known Issues¶
1. SET of a local variable with property name¶
This type of set is not currently supported by Snowflake scripting.
2. SET of a local variable with mutator method¶
This type of set is not currently supported by Snowflake scripting.
Related EWIs¶
SSC-EWI-TS0037: Snowflake Scripting Cursors are non-scrollable.
SSC-EWI-0073: Pending Functional Equivalence Review.
SSC-FDM-TS0013: Snowflake Scripting cursor rows are not modifiable.
TRY CATCH¶
Translation reference for TRY CATCH statement in Transact-SQL.
Applies to
SQL Server
Azure Synapse Analytics
Description¶
Implements error handling for Transact SQL. A group of Transact-SQL statements can be enclosed in a TRY block. If an error occurs in the TRY block, control is usually passed to another group of statements that is enclosed in a CATCH block.
Sample Source Patterns¶
The following example details the transformation for TRY CATCH inside procedures.
Transact-SQL¶
Output¶
Snowflake SQL¶
Output¶
Try catch outside routines (functions and procedures)¶
Transact-SQL¶
Snowflake Scripting¶
Related EWIs¶
SSC-FDM-0020: Multiple result sets are returned in temporary tables.
WHILE¶
Translation reference to convert Transact-SQL While Statement to Snowflake Scripting
Applies to
SQL Server
Azure Synapse Analytics
Description¶
The While statement allows an SQL statement or a block of statements to be repeatedly executed as long as the specified condition is true. The execution of statements in the WHILE loop can be controlled from inside the loop with the BREAK and CONTINUE keywords.
For more information, see the Transact-SQL WHILE documentation.
Note: To define a statement block, use the control-of-flow keywords BEGIN and END.
Sample Source Patterns¶
Basic source pattern code¶
Transact-SQL¶
The following code refers to a While Loop in Transact-SQL that iterates the @Iteration variable and controls the flow of the loop to terminate when the value of @Iteration equals 10.
Note
Statements after the CONTINUE keyword will not be executed.
While¶
Result¶
iteration |
|---|
10 |
Snowflake Scripting¶
Note
As well as Transact-SQL, in Snowflake Scripting the statements after the CONTINUE keyword will not be executed.
Notice that in Snowflake Scripting it is not necessary to use the BEGIN and END keywords to define a statement block, however it can be used if required.
While¶
Loop keyword¶
Snowflake Scripting allows to use LOOP keyword instead of DO and the END LOOP expression instead of END WHILE .
Result¶
Iteration |
|---|
10 |
While with empty body Source Pattern¶
Transact-SQL¶
Note
Please note this example was written while the IF ELSE statement was not supported, the differences in the results should disappear when support for the statement is implemented.
Result¶
result |
|---|
125 |
Snowflake Scripting¶
This statement can not have an empty body in Snowflake Scripting, to solve this cases a default BREAK statement is added when an empty body is detected.
Result¶
result |
|---|
1 |
WHILE statement outside routines (functions and procedures)¶
Unlike Transact-SQL, Snowflake does not support executing isolated statements like WHILE outside routines like functions or procedures. For this scenario, the statement should be encapsulated in an anonymous block, as shown in the following example.
Transact-SQL¶
Snowflake Scripting¶
Related EWIs¶
SSC-EWI-0073: Pending Functional Equivalence Review.