SP_EXECUTESQL
Translation specification for the system procedure SP_EXECUTESQL.
Description
The SP_EXECUTESQL system stored procedure is used to execute a Transact-SQL statement or batch that can be reused many times, or one that is built dynamically. The statement or batch can contain embedded parameters.
This functionality can be emulated in Snowflake through the EXECUTE IMMEDIATE statement and with a user-defined function (UDF) for embedded parameters.
For more information about the user-defined function (UDF) used for this translation, check TRANSFORM_SP_EXECUTE_SQL_STRING_UDF(STRING, STRING, ARRAY, ARRAY).
Syntax
sp_executesql [ @stmt = ] N'statement'
[
[ , [ @params = ] N'@parameter_name data_type [ { OUT | OUTPUT } ] [ , ...n ]' ]
[ , [ @param1 = ] 'value1' [ , ...n ] ]
]Sample Source Patterns
All patterns will transform SP_EXECUTESQL into Snowflake's EXECUTE IMMEDIATE statement and only modify the SQL string to be executed when using embedded parameters.
SSC-EWI-0030 (Usage of Dynamic SQL) will be added for all patterns. Even though the translation for SP_EXECUTESQL is equivalent to Snowflake, in this context, this EWI indicates that the SQL string might require manual fixes for the translation to execute as intended.
Setup Data
Without embedded parameters
When no embedded parameters are being used, the SP_EXECUTESQL is transformed into an EXECUTE IMMEDIATE statement and use the SQL string without modifications.
Transact
John Smith
1
24
John Doe
2
21
Mary Keller
3
32
Mundane Man
4
18
Snowflake
John Smith
1
24
John Doe
2
21
Mary Keller
3
32
Mundane Man
4
18
With embedded parameters for data binding
For embedded parameters for data binding, the SP_EXECUTESQL is transformed into an EXECUTE IMMEDIATE statement, and the SQL string is modified through the TRANSFORM_SP_EXECUTE_SQL_STRING_UDF.
The result of the EXECUTE IMMEDIATE is assigned to the ProcedureResultSet variable and later returned as TABLE(ProcedureResultSet).
Transact
John Doe
2
21
Snowflake
John Doe
2
21
With embedded OUTPUT parameters
For embedded OUTPUT parameters, the SP_EXECUTESQL is transformed into an EXECUTE IMMEDIATE statement, and the SQL string is modified through the TRANSFORM_SP_EXECUTE_SQL_STRING_UDF.
Additionally, a SELECT $1, ..., $n INTO :outputParam1, ..., :outputParamN FROM TABLE(RESULT_SCAN(LAST_QUERY_ID())) is added to the result of each column to the corresponding OUTPUT parameter.
SSC-FDM-TS0028 is added to the SELECT INTO statement. It is essential for the parameters in the INTO clause to appear in the same order as they were assigned in the original SQL String.
Otherwise, manual changes are required to meet this requirement.
Transact
32
Snowflake
32
With both embedded OUTPUT parameters and data binding
The translation is the same as for only OUTPUT parameters.
Transact
24
4
Snowflake
24
4
Parameters not in order of definition
This pattern follows the same rules as the previous patterns. TRANSFORM_SP_EXECUTE_SQL_STRING_UDF replaces the parameter values in the correct order.
Transact
24
4
24
4
Snowflake
24
4
24
4
Execute direct values
This translation also handles the cases where the values a directly assigned instead of using variables.
Transact
24
4
Snowflake
24
4
SQL string dynamically built
This pattern follows the same rules as the previous patterns. However, assigning the result of the EXECUTE IMMEDIATE statement might not be added if the SQL string is not a simple single query with or without embedded parameters.
Furthermore, the SQL string must start with the literal value 'SELECT' for SnowConvert to correctly identify that a SELECT statement is going to be executed. For more information, check the Known Issues section.
Transact
John Doe
2
21
Snowflake
John Doe
2
21
Returning multiple result sets
Snowflake Scripting procedures only allow one result set to be returned per procedure.
To replicate Transact-SQL behavior, when two or more result sets are to be returned, they are stored in temporary tables. The Snowflake Scripting procedure will return an array containing the names of the temporary tables. For more information, check SSC-FDM-0020.
Transact
John Doe
2
21
John Smith
1
24
John Doe
2
21
Mary Keller
3
32
Mundane Man
4
18
INSERT FIRST
1200
230
Snowflake
[ "RESULTSET_88C35D7A_1E5B_455D_97A4_247806E583A5", "RESULTSET_B2345B61_A015_43CB_BA11_6D3E013EF262" ]
Known Issues
1. Invalid code is detected
SP_EXECUTESQL can execute more than one SQL statement inside the SQL string. Snowflake also supports executing multiple SQL statements, but need to be enclosed in a BEGIN ... END block. Furthermore, when executing multiple statements from a BEGIN ... END block, the EXECUTE IMMEDIATE will not return a resultset. The translation for these cases is not yet supported by SnowConvert. For more information, check SSC-EWI-0030.
Thus, when this case is detected, in the translated code, the EXECUTE IMMEDIATE will not be assigned to the ProcedureResultSet.
Transact
John Smith
1
24
John Doe
2
21
Mary Keller
3
32
Mundane Man
4
18
INSERT FIRST
1200
230
Snowflake
2. Valid or Invalid code is not detected
When the SQL string is built dynamically through concatenations, SnowConvert might not detect what statement is going to be executed. Thus, in the translated code, the EXECUTE IMMEDIATE will not be assigned to the ProcedureResultSet.
Transact
John Smith
1
24
John Doe
2
21
Mary Keller
3
32
Mundane Man
4
18
INSERT FIRST
1200
230
Snowflake
3. Invalid code is mistaken as valid
If the SQL string starts with a SELECT statement and is followed by more statements, SnowConvert will detect this as a valid code and try to assign the result of the EXECUTE IMMEDIATE to theProcedureResultSet. This leads to a compilation error. For more information, check SSC-EWI-0030.
Transact
John Smith
1
24
John Doe
2
21
Mary Keller
3
32
Mundane Man
4
18
John Smith
1
24
John Doe
2
21
Mary Keller
3
32
Mundane Man
4
18
Snowflake
Related EWIs
SSC-EWI-0030: The statement below has usages of dynamic SQL
SSC-FDM-TS0028: Output parameters must have the same order as they appear in the executed code.
SSC-FDM-0020: Multiple result sets are returned in temporary tables.
Last updated
