SnowConvert AI - SQL Server - CREATE FUNCTION¶
Translation reference for the Transact-SQL User Defined Functions
Applies to
SQL Server
Azure Synapse Analytics
Description¶
SQL Server only supports two types of User Defined Functions:
Using these UDFs types, is possible to subcategorized them into simple and complex, according to the inner logic.
Simple UDFs, matches the SQL Server syntax with Snowflake syntax. This type doesn’t add any logic and goes straightforward to the result. These are usually match to Snowflake’s SQL UDFs.
SnowConvert supports translating SQL Server Scalar User Defined Functions directly to Snowflake Scripting UDFs when they meet specific criteria.
Complex UDFs, makes extensive use of a particular statements (INSERT, DELETE, UPDATE, SET, DECLARE, etc) or control-of-flow blocks (IF…ELSE, WHILE, etc) and usually represents a mismatch or violation to Snowflake’s SQL UDFs definition.
Limitations¶
Transact UDFs have some limitations not present in other database engines (such as Oracle and Teradata). These limitations helps the translations by narrowing the failure scope. This means, there are specific scenarios we can expect to avoid.
Here are some of the limitations SQL Server has on UDFs
UDFs cannot be used to perform actions that modify the database state
User-defined functions cannot contain an OUTPUT INTO clause that has a table as its target
User-defined functions cannot return multiple result sets. Use a stored procedure if you need to return multiple result sets.
For the full list, please check this link Create User-defined Functions (Database engine)
INLINE TABLE-VALUED¶
Translation reference to convert Transact-SQL UDF (User Defined Functions) with TABLE return type to Snowflake.
Applies to
SQL Server
Azure Synapse Analytics
Description¶
Note
Some parts in the output code are omitted for clarity reasons.
Inline Table-Valued functions are table expression that can accept parameters, perform a SELECT statement and return a TABLE (SQL Server Language Reference Creating an inline table-valued function).
Transact Syntax¶
Snowflake SQL Syntax¶
Sample Source Patterns¶
The following section describes all the possible source code patterns that can appear in this kind of CREATE FUNCTION syntax.
For Inline Table-Valued functions, there can only exist one statement per body that could be:
SELECTStatementWITHCommon Table Expression
Select and return values directly from one table¶
This is the simplest scenario, performing a simple select from a table and returning those values
Transact-SQL¶
Inline Table-Valued¶
Result¶
DepartmentID |
Name |
GroupName |
|---|---|---|
1 |
Engineering |
Research and Development |
2 |
Tool Design |
Research and Development |
3 |
Sales |
Sales and Marketing |
4 |
Marketing |
Sales and Marketing |
5 |
Purchasing |
Inventory Management |
6 |
Research and Development |
Research and Development |
7 |
Production |
Manufacturing |
8 |
Production Control |
Manufacturing |
9 |
Human Resources |
Executive General and Administration |
10 |
Finance |
Executive General and Administration |
11 |
Information Services |
Executive General and Administration |
12 |
Document Control |
Quality Assurance |
13 |
Quality Assurance |
Quality Assurance |
14 |
Facilities and Maintenance |
Executive General and Administration |
15 |
Shipping and Receiving |
Inventory Management |
16 |
Executive |
Executive General and Administration |
Snowflake SQL¶
Inline Table-Valued¶
Result¶
DepartmentID |
Name |
GroupName |
|---|---|---|
1 |
Engineering |
Research and Development |
2 |
Tool Design |
Research and Development |
3 |
Sales |
Sales and Marketing |
4 |
Marketing |
Sales and Marketing |
5 |
Purchasing |
Inventory Management |
6 |
Research and Development |
Research and Development |
7 |
Production |
Manufacturing |
8 |
Production Control |
Manufacturing |
9 |
Human Resources |
Executive General and Administration |
10 |
Finance |
Executive General and Administration |
11 |
Information Services |
Executive General and Administration |
12 |
Document Control |
Quality Assurance |
13 |
Quality Assurance |
Quality Assurance |
14 |
Facilities and Maintenance |
Executive General and Administration |
15 |
Shipping and Receiving |
Inventory Management |
16 |
Executive |
Executive General and Administration |
Select and return values from multiple tables renaming columns and using built in functions¶
This is an example of a query using built-in functions in a select statement getting data from different tables, renaming columns and returning a table.
Transact-SQL¶
Inline Table-Valued¶
Result¶
PersonType |
FirstName |
JobTitle |
Gender |
HIREYEAR |
|---|---|---|---|---|
EM |
Ken |
Chief Executive Officer |
M |
2009 |
EM |
Terri |
Vice President of Engineering |
F |
2008 |
EM |
Roberto |
Engineering Manager |
M |
2007 |
EM |
Rob |
Senior Tool Designer |
M |
2007 |
EM |
Gail |
Design Engineer |
F |
2008 |
EM |
Jossef |
Design Engineer |
M |
2008 |
EM |
Dylan |
Research and Development Manager |
M |
2009 |
EM |
Diane |
Research and Development Engineer |
F |
2008 |
EM |
Gigi |
Research and Development Engineer |
F |
2009 |
EM |
Michael |
Research and Development Manager |
M |
2009 |
EM |
Ovidiu |
Senior Tool Designer |
M |
2010 |
EM |
Thierry |
Tool Designer |
M |
2007 |
EM |
Janice |
Tool Designer |
F |
2010 |
EM |
Michael |
Senior Design Engineer |
M |
2010 |
EM |
Sharon |
Design Engineer |
F |
2011 |
EM |
David |
Marketing Manager |
M |
2007 |
EM |
Kevin |
Marketing Assistant |
M |
2007 |
EM |
John |
Marketing Specialist |
M |
2011 |
EM |
Mary |
Marketing Assistant |
F |
2011 |
EM |
Wanida |
Marketing Assistant |
F |
2011 |
Snowflake SQL¶
Inline Table-Valued¶
Result¶
PersonType |
FirstName |
JobTitle |
Gender |
HIREYEAR |
|---|---|---|---|---|
EM |
Ken |
Chief Executive Officer |
M |
2009 |
EM |
Terri |
Vice President of Engineering |
F |
2008 |
EM |
Roberto |
Engineering Manager |
M |
2007 |
EM |
Rob |
Senior Tool Designer |
M |
2007 |
EM |
Gail |
Design Engineer |
F |
2008 |
EM |
Jossef |
Design Engineer |
M |
2008 |
EM |
Dylan |
Research and Development Manager |
M |
2009 |
EM |
Diane |
Research and Development Engineer |
F |
2008 |
EM |
Gigi |
Research and Development Engineer |
F |
2009 |
EM |
Michael |
Research and Development Manager |
M |
2009 |
EM |
Ovidiu |
Senior Tool Designer |
M |
2010 |
EM |
Thierry |
Tool Designer |
M |
2007 |
EM |
Janice |
Tool Designer |
F |
2010 |
EM |
Michael |
Senior Design Engineer |
M |
2010 |
EM |
Sharon |
Design Engineer |
F |
2011 |
EM |
David |
Marketing Manager |
M |
2007 |
EM |
Kevin |
Marketing Assistant |
M |
2007 |
EM |
John |
Marketing Specialist |
M |
2011 |
EM |
Mary |
Marketing Assistant |
F |
2011 |
EM |
Wanida |
Marketing Assistant |
F |
2011 |
Select columns using WITH statement¶
The body of an inline table-valued function can also be specified using a WITH statement as shown below.
Transact-SQL¶
Inline Table-Valued¶
Result¶
MaritalStatus |
Gender |
Name |
|---|---|---|
S |
F |
Terri Duffy |
M |
F |
Gail Erickson |
S |
F |
Diane Margheim |
M |
F |
Gigi Matthew |
M |
F |
Janice Galvin |
M |
F |
Sharon Salavaria |
S |
F |
Mary Dempsey |
M |
F |
Wanida Benshoof |
M |
F |
Mary Gibson |
M |
F |
Jill Williams |
S |
F |
Jo Brown |
M |
F |
Britta Simon |
M |
F |
Margie Shoop |
M |
F |
Rebecca Laszlo |
M |
F |
Suchitra Mohan |
M |
F |
Kim Abercrombie |
S |
F |
JoLynn Dobney |
M |
F |
Nancy Anderson |
M |
F |
Ruth Ellerbrock |
M |
F |
Doris Hartwig |
M |
F |
Diane Glimp |
M |
F |
Bonnie Kearney |
M |
F |
Denise Smith |
S |
F |
Diane Tibbott |
M |
F |
Carole Poland |
M |
F |
Carol Philips |
M |
F |
Merav Netz |
S |
F |
Betsy Stadick |
S |
F |
Danielle Tiedt |
S |
F |
Kimberly Zimmerman |
M |
F |
Elizabeth Keyser |
M |
F |
Mary Baker |
M |
F |
Alice Ciccu |
M |
F |
Linda Moschell |
S |
F |
Angela Barbariol |
S |
F |
Kitti Lertpiriyasuwat |
S |
F |
Susan Eaton |
S |
F |
Kim Ralls |
M |
F |
Nicole Holliday |
S |
F |
Anibal Sousa |
M |
F |
Samantha Smith |
S |
F |
Olinda Turner |
S |
F |
Cynthia Randall |
M |
F |
Sandra Reátegui Alayo |
S |
F |
Linda Randall |
S |
F |
Shelley Dyck |
S |
F |
Laura Steele |
S |
F |
Susan Metters |
S |
F |
Katie McAskill-White |
M |
F |
Barbara Decker |
M |
F |
Yvonne McKay |
S |
F |
Janeth Esteves |
M |
F |
Brenda Diaz |
M |
F |
Lorraine Nay |
M |
F |
Paula Nartker |
S |
F |
Lori Kane |
M |
F |
Kathie Flood |
S |
F |
Belinda Newman |
M |
F |
Karen Berge |
M |
F |
Lori Penor |
M |
F |
Jo Berry |
M |
F |
Laura Norman |
M |
F |
Paula Barreto de Mattos |
M |
F |
Mindy Martin |
M |
F |
Deborah Poe |
S |
F |
Candy Spoon |
M |
F |
Barbara Moreland |
M |
F |
Janet Sheperdigian |
S |
F |
Wendy Kahn |
S |
F |
Sheela Word |
M |
F |
Linda Meisner |
S |
F |
Erin Hagens |
M |
F |
Annette Hill |
S |
F |
Jean Trenary |
S |
F |
Stephanie Conroy |
S |
F |
Karen Berg |
M |
F |
Janaina Bueno |
M |
F |
Linda Mitchell |
S |
F |
Jillian Carson |
S |
F |
Pamela Ansman-Wolfe |
S |
F |
Lynn Tsoflias |
M |
F |
Amy Alberts |
S |
F |
Rachel Valdez |
M |
F |
Jae Pak |
Snowflake SQL¶
Inline Table-Valued¶
Result¶
MaritalStatus |
Gender |
Name |
|---|---|---|
S |
F |
Terri Duffy |
M |
F |
Gail Erickson |
S |
F |
Diane Margheim |
M |
F |
Gigi Matthew |
M |
F |
Janice Galvin |
M |
F |
Sharon Salavaria |
S |
F |
Mary Dempsey |
M |
F |
Wanida Benshoof |
M |
F |
Mary Gibson |
M |
F |
Jill Williams |
S |
F |
Jo Brown |
M |
F |
Britta Simon |
M |
F |
Margie Shoop |
M |
F |
Rebecca Laszlo |
M |
F |
Suchitra Mohan |
M |
F |
Kim Abercrombie |
S |
F |
JoLynn Dobney |
M |
F |
Nancy Anderson |
M |
F |
Ruth Ellerbrock |
M |
F |
Doris Hartwig |
M |
F |
Diane Glimp |
M |
F |
Bonnie Kearney |
M |
F |
Denise Smith |
S |
F |
Diane Tibbott |
M |
F |
Carole Poland |
M |
F |
Carol Philips |
M |
F |
Merav Netz |
S |
F |
Betsy Stadick |
S |
F |
Danielle Tiedt |
S |
F |
Kimberly Zimmerman |
M |
F |
Elizabeth Keyser |
M |
F |
Mary Baker |
M |
F |
Alice Ciccu |
M |
F |
Linda Moschell |
S |
F |
Angela Barbariol |
S |
F |
Kitti Lertpiriyasuwat |
S |
F |
Susan Eaton |
S |
F |
Kim Ralls |
M |
F |
Nicole Holliday |
S |
F |
Anibal Sousa |
M |
F |
Samantha Smith |
S |
F |
Olinda Turner |
S |
F |
Cynthia Randall |
M |
F |
Sandra Reátegui Alayo |
S |
F |
Linda Randall |
S |
F |
Shelley Dyck |
S |
F |
Laura Steele |
S |
F |
Susan Metters |
S |
F |
Katie McAskill-White |
M |
F |
Barbara Decker |
M |
F |
Yvonne McKay |
S |
F |
Janeth Esteves |
M |
F |
Brenda Diaz |
M |
F |
Lorraine Nay |
M |
F |
Paula Nartker |
S |
F |
Lori Kane |
M |
F |
Kathie Flood |
S |
F |
Belinda Newman |
M |
F |
Karen Berge |
M |
F |
Lori Penor |
M |
F |
Jo Berry |
M |
F |
Laura Norman |
M |
F |
Paula Barreto de Mattos |
M |
F |
Mindy Martin |
M |
F |
Deborah Poe |
S |
F |
Candy Spoon |
M |
F |
Barbara Moreland |
M |
F |
Janet Sheperdigian |
S |
F |
Wendy Kahn |
S |
F |
Sheela Word |
M |
F |
Linda Meisner |
S |
F |
Erin Hagens |
M |
F |
Annette Hill |
S |
F |
Jean Trenary |
S |
F |
Stephanie Conroy |
S |
F |
Karen Berg |
M |
F |
Janaina Bueno |
M |
F |
Linda Mitchell |
S |
F |
Jillian Carson |
S |
F |
Pamela Ansman-Wolfe |
S |
F |
Lynn Tsoflias |
M |
F |
Amy Alberts |
S |
F |
Rachel Valdez |
M |
F |
Jae Pak |
Known issues¶
No issues were found
MULTI-STATEMENT TABLE-VALUED¶
Translation reference to convert Transact-SQL UDF (User Defined Functions) with TABLE return type to Snowflake.
Applies to
SQL Server
Azure Synapse Analytics
Note
Some parts in the output code are omitted for clarity reasons.
Note
All the code samples on this page have not been implemented yet in SnowConvert AI. They should be interpreted as a reference for how each scenario should be translated to Snowflake. These translations may change in the future.Some parts in the output code are omitted for clarity reasons.
Description¶
Multi-statement table-valued is similar to Inline-statement table-valued (INLINE TABLE-VALUED). However Multi-statement table-valued may have more than one statement in its function body, the table columns are specified in the return type and it has a BEGIN/END block (SQL Server Language Reference Creating a multi-statement table-valued function
Transact-SQL Syntax¶
Snowflake SQL¶
Sample Source Patterns¶
The following section describes all the possible source code patterns that can appear in this kind ofCREATE FUNCTION syntax.
The function body of Multi-Statement Table-Valued function must be a SELECT statement. For this reason the others statements must be called separately.
Insert values in a table¶
Inserts one or more rows into the table and returns the table with the new values
Transact-SQL¶
MULTI-STATEMENT TABLE-VALUED¶
Result¶
BEHAVIORAL_SEGMENT |
|---|
Unknown |
Snowflake SQL¶
MULTI-STATEMENT TABLE-VALUED¶
Results¶
BEHAVIORAL_SEGMENT |
|---|
Unknown |
Insert value according to if/else statement¶
Inserts a row into the table according to the condition and returns the table with the new value
Transact-SQL¶
MULTI-STATEMENT TABLE-VALUED¶
Result¶
NUMBER_TYPE |
|---|
Odd |
Snowflake SQL¶
MULTI-STATEMENT TABLE-VALUED¶
Result¶
NUMBER_TYPE |
|---|
Odd |
Inserts multiple according to if/else statement¶
The example below inserts more than one value into the table and more than one variable is modified according to the condition. Returns the table with the new values
Transact-SQL¶
MULTI-STATEMENT TABLE-VALUED¶
Result¶
ID_EMPLOYEE |
WORKING_FROM_HOME |
TEAM |
COMPUTER |
|---|---|---|---|
123456789 |
1 |
TEAM_1 |
LAPTOP |
Snowflake¶
MULTI-STATEMENT TABLE-VALUED¶
Result¶
ID_EMPLOYEE |
WORKING_FROM_HOME |
TEAM |
COMPUTER |
|---|---|---|---|
123456789 |
1 |
TEAM_1 |
LAPTOP |
Warning
In case there are nested if statements and more than one variables are modified in the statements it is necessary to use a stored procedure.
Update values previously inserted¶
Updates columns values of the table into the function body and returns it with the new values.
Transact-SQL¶
MULTI-STATEMENT TABLE-VALUED¶
Result¶
DEPARTMENT_NAME |
FIRST_NAME |
LAST_NAME |
START_DATE |
END_DATE |
JOB_TITLE |
MONTHS_WORKING |
|---|---|---|---|---|---|---|
Sales |
Syed |
Abbas |
2013-03-14 |
NULL |
Pacific Sales Manager |
106 |
Production |
Kim |
Abercrombie |
2010-01-16 |
NULL |
Production Technician - WC60 |
144 |
Quality Assurance |
Hazem |
Abolrous |
2009-02-28 |
NULL |
Quality Assurance Manager |
155 |
Shipping and Receiving |
Pilar |
Ackerman |
2009-01-02 |
NULL |
Shipping and Receiving Supervisor |
156 |
Production |
Jay |
Adams |
2009-03-05 |
NULL |
Production Technician - WC60 |
154 |
Information Services |
François |
Ajenstat |
2009-01-17 |
NULL |
Database Administrator |
156 |
Sales |
Amy |
Alberts |
2012-04-16 |
NULL |
European Sales Manager |
117 |
Production |
Greg |
Alderson |
2008-12-02 |
NULL |
Production Technician - WC45 |
157 |
Quality Assurance |
Sean |
Alexander |
2008-12-28 |
NULL |
Quality Assurance Technician |
157 |
Facilities and Maintenance |
Gary |
Altman |
2009-12-02 |
NULL |
Facilities Manager |
145 |
Snowflake SQL¶
MULTI-STATEMENT TABLE-VALUED¶
Result¶
DEPARTMENT_NAME |
FIRST_NAME |
LAST_NAME |
START_DATE |
END_DATE |
JOB_TITLE |
MONTHS_WORKING |
|---|---|---|---|---|---|---|
Sales |
Syed |
Abbas |
2013-03-14 |
NULL |
Pacific Sales Manager |
106 |
Production |
Kim |
Abercrombie |
2010-01-16 |
NULL |
Production Technician - WC60 |
144 |
Quality Assurance |
Hazem |
Abolrous |
2009-02-28 |
NULL |
Quality Assurance Manager |
155 |
Shipping and Receiving |
Pilar |
Ackerman |
2009-01-02 |
NULL |
Shipping and Receiving Supervisor |
156 |
Production |
Jay |
Adams |
2009-03-05 |
NULL |
Production Technician - WC60 |
154 |
Information Services |
François |
Ajenstat |
2009-01-17 |
NULL |
Database Administrator |
156 |
Sales |
Amy |
Alberts |
2012-04-16 |
NULL |
European Sales Manager |
117 |
Production |
Greg |
Alderson |
2008-12-02 |
NULL |
Production Technician - WC45 |
157 |
Quality Assurance |
Sean |
Alexander |
2008-12-28 |
NULL |
Quality Assurance Technician |
157 |
Facilities and Maintenance |
Gary |
Altman |
2009-12-02 |
NULL |
Facilities Manager |
145 |
Multiple return clauses¶
In the following sample there is more than one return clause, this is because depending on the situation it is not necessary to keep executing the whole function.
Transact-SQL¶
MULTI-STATEMENT TABLE-VALUED¶
Result¶
TYPE |
NAME |
|---|---|
SMALL_TEAM |
TEAM1 |
Snowflake SQL¶
MULTI-STATEMENT TABLE-VALUED¶
Result¶
TYPE |
NAME |
|---|---|
SMALL_TEAM |
TEAM1 |
Warning
This transformation is applied when there is only one value to insert, if there is more than one value it is necessary to use a stored procedure.
Complex cases¶
The example is a complex case that uses nested if statements and inserts a value depending on the true condition.
Transact-SQL¶
MULTI-STATEMENT TABLE-VALUED¶
Result¶
VACATION_STATUS |
|---|
OK |
Snowflake SQL¶
MULTI-STATEMENT TABLE-VALUED¶
Second Tab¶
VACATION_STATUS |
|---|
OK |
Known Issues¶
While statements along side queries¶
The problem with this example is that there’s no way of transforming the while statement to a CTE inside the WITH clause of the main select, this forces us to transform this statement to store procedure to maintain the same logic.
Transact-SQL¶
MULTI-STATEMENT TABLE-VALUED¶
Result¶
GROUP_NAME |
|---|
Tool Design |
Snowflake SQL¶
MULTI-STATEMENT TABLE-VALUED¶
Result¶
GROUP_NAME |
|---|
Tool Design |
Declare Cursor¶
User-defined functions cannot DECLARE, OPEN, FETCH, CLOSE or DEALLOCATE a CURSOR. Use a Stored Procedure to work with cursors.
Transact-SQL¶
MULTI-STATEMENT TABLE-VALUED¶
Result¶
AMOUNT |
|---|
3 |
Snowflake SQL¶
MULTI-STATEMENT TABLE-VALUED¶
Result¶
AMOUNT |
|---|
3 |
Different statements are not supported in Common Tables Expressions¶
The clauses UPDATE, INSERT, DELETE, ALTER or DROP are not supported on the body of common tables expressions, even after their declaration using a delimitator. For this reason, the function can be modified to work as a stored procedure.
Transact-SQL¶
MULTI-STATEMENT TABLE-VALUED¶
Result¶
PRODUCT_NAME |
Rating |
|---|---|
HL Mountain Pedal |
3 |
Mountain Bike Socks, M |
5 |
Road-550-W Yellow, 40 |
5 |
Snowflake SQL¶
MULTI-STATEMENT TABLE-VALUED¶
Result¶
PRODUCT_NAME |
Rating |
|---|---|
HL Mountain Pedal |
3 |
Mountain Bike Socks, M |
5 |
Road-550-W Yellow, 40 |
5 |
Related EWIs¶
SSC-EWI-0040: Statement Not Supported.
SSC-EWI-0073: Pending Functional Equivalence Review
SCALAR¶
Translation reference to convert Transact-SQL UDF (User Defined Functions) with scalar return type to Snowflake.
Applies to
SQL Server
Azure Synapse Analytics
Description¶
Note
Some parts in the output code are omitted for clarity reasons.
A scalar user-defined function is a Transact-SQL or common language runtime (CLR) routine that accepts parameters, performs an action, such as a complex calculation, and returns the result of that action as a scalar value. (SQL Server Language ReferenceCREATE FUNCTION subsection).
Note
These functions are usually used inside the SELECTstatement, or single variable setup (most likely inside a stored procedure).
Transact-SQL Syntax¶
Snowflake Syntax¶
Snowflake allows 3 different languages in their user defined functions:
SQL
JavaScript
Java
For now, SnowConvert AI will support only SQL and JavaScript as target languages.
SQL¶
Note
SQL user defined functions only supports one query as their body. They can read from the database, but is not allowed to write or modify it. (Scalar SQL UDFs Reference).
JavaScript¶
Note
JavaScript user defined functions allows multiple statements in their bodies, but cannot perform queries to the database. (Scalar JavaScript UDFs Reference)
Sample Source Patterns¶
Set and Declare Statements¶
The most common statements in function bodies are the DECLARE and SET statements. For DECLARE statements without default value, the transformation will be ignored. SET statements and DECLARE statements with a default value, will be transformed to a COMMON TABLE EXPRESSION. Each common table expression will contain a column that represents the local variable value.
Transact-SQL¶
Query¶
Result¶
vendor_name |
|---|
Australia Bike Retailer |
Snowflake¶
Query¶
Result¶
VENDOR_NAME |
|---|
Australia Bike Retailer |
If/Else Statement Transformation¶
If/Else statement can be handled in different ways, they can be either transformed to javascript or to SQL using the CASE EXPRESSION inside the select allowing conditionals inside the queries, while the javascript transformation is pretty straightforward, the Case statement might not be so obvious at first glance.
Transact-SQL¶
Query¶
Result¶
has_active_flag |
|---|
NO |
Snowflake¶
Query¶
Result¶
HAS_ACTIVE_FLAG |
|---|
NO |
Nested Statements¶
For nested statements, the structured programming is being transformed to a single query. The statements in the control-of-flow are going to be nested in table structures to preserve the execution order.
Note
CASE EXPRESSIONS only can return one value per statement
Example¶
Note
The following code in both programming paradigms is functionally equivalent.
Structured Programming¶
SQL¶
Result¶
AccountNumber |
|---|
LITWARE0001 |
Conditional variables through SELECTs¶
Variable definition and assignment within conditional statements tends to be somewhat problematic, because references to the variable further down the code would have to know where the variable was last modified. Not only that, but if the reference is within another conditional statement, then there would have to be some kind of redirect that references the previous known assignment to the variable.
This is all aggravated by nesting and complex querying that can be found on input code. That’s why a specific EWI is added when these patterns are found.
In the following scenario, the first IF statement can be transformed without problems, because the contents are straightforward enough. The second and third IF statements are commented out because they’re not supported at the moment, since there are statements other than variable assignments through SELECT.
SQL Server¶
Query¶
Result¶
RESULT |
|---|
10 |
Snowflake¶
Query¶
Result¶
RESULT |
|---|
10 |
Assign and return a variable¶
In this simple pattern, there is a variable declaration, then, that variable is set using a SELECT statement and finally returned. This is going to be migrated to a Common Table Expression to keep the original behavior.
SQL Server¶
Query¶
Result¶
Result |
|---|
1583978.2263 |
Snowflake¶
Query¶
Result¶
RESULT |
|---|
1583978.2263 |
Multiple Function Calls¶
For this specific pattern there are no obvious queries, but there are multiple calls to multiple functions working on the same variable and returning it at the end. Since Snowflake only supports queries inside its functions, the solution for this block is going to be adding it to a Select and nesting the calls inside, making sure the return value is the same as the one on the source.
SQL Server¶
Query¶
Result¶
Name |
|---|
USA Car Retailer |
Snowflake¶
Query¶
Result¶
NAME |
|---|
USA Car Retailer |
Increase a variable based on multiple IF conditions and return its value¶
For this pattern, a variable is modified (increased in this case) using multiple IF conditions. In the beginning, a set of variables is initialized and used to determine whether the result variable should be increased or not. Finally, the result variable is returned.
SQL Server¶
Query¶
Result¶
Result |
|---|
473.1415 |
Snowflake¶
Query¶
Result¶
RESULT |
|---|
473.1415 |
Two or more RETURN statements¶
For this pattern, the IF block containing the return clause that breaks the code flow is added at the end of the body, like the final statement to be executed in a CASE expression.
Basic Case¶
For this particular scenario, there is no logic between the conditional RETURN statement and the final RETURN statement, so all body will be mapped to a single CASE EXPRESSION.
SQL Server¶
Query¶
Result¶
result |
|---|
1 |
Snowflake¶
Query¶
Result¶
RESULT |
|---|
1 |
Common Table Expressions¶
Common table expressions will be kept as in the original code, and they are going to be concatenated with the generated ones. SnowConvert AI is able to identify first all the original COMMON TABLE EXPRESSION names to avoid generating duplicated names.
SQL Server¶
Query¶
Result¶
result |
|---|
3689 |
Snowflake¶
Query¶
Result¶
RESULT |
|---|
3689 |
Transform to JavaScript UDFs¶
If there are multiple statements and the function does not access the database in any way, it can be transformed into a JavaScript function keeping the functional equivalence
SQL Server¶
Query 1¶
Query 2¶
Result 1¶
DATE |
|---|
2021 |
Result 2¶
CleanChargeCode |
|---|
16 |
Snowflake¶
Query 1¶
Query 2¶
Result 1¶
DATE |
|---|
2021.0 |
Result 2¶
CLEANCHARGECODE |
|---|
16 |
Known Issues¶
Warning
User-defined functions cannot be used to perform actions that modify the database state
Warning
User-defined functions cannot contain an OUTPUT INTO clause that has a table as its target
Warning
User-defined functions cannot DECLARE, OPEN, FETCH, CLOSE or DEALLOCATE a CURSOR. Use a Stored Procedure if you need to use cursors.
Warning
User-defined functions cannot perform control-of-flow statements such as WHILE if there is at least one call to the database
Warning
User-defined functions with references to other user-defined functions that were transformed to Stored Procedures, will be transformed to Stored Procedures too.
Warning
User-defined functions that use @@ROWCOUNT are not supported in SQL and should be transformed to stored procedures to keep the functional equivalence.
Warning
User-defined functions that have SELECT statements assigning a variable to itself is not supported in Snowflake. See also SELECT @local_variable
For all the unsupported cases, please check the related EWIs and the patterns below to obtain recommendations and possible workarounds.
Conditionals other than if/else statements along side queries¶
The next scenario involves the use of the “while statement” along side other queries. The problem with this example is that there’s no way of transforming the while statement to a CTE inside the WITH clause of the main select, this forces us to transform this statement to JavaScript procedure to maintain the same logic.
SQL Server¶
Query¶
Result¶
result |
|---|
1007 |
Snowflake
Query¶
Result¶
FOO |
|---|
1007 |
Assign a variable using its own value iterating through a rowset¶
In the following example, the variable @names is used to concatenate multiple values from a column into one single string. The variable is updated on each iteration as shown, which is not supported by Snowflake UDFs. For this scenario, the function should be transformed into a procedure.
SQL Server
Query¶
Result¶
names |
|---|
Australia Bike Retailer Allenson Cycles Advanced Bicycles Trikes, Inc. Morgan Bike Accessories Cycling Master Chicago Rent-All Greenwood Athletic Company Compete Enterprises, Inc International Light Speed Training Systems Gardner Touring Cycles Internati |
Snowflake query
Warning
For the described scenarios above, consider the following limitations:
All the calls to user-defined functions in DML queries such as
SELECT,INSERT,DELETE,UPDATEorMERGEwill fail because calls to Stored Procedures within these queries are not allowed.Calls to user-defined functions inside procedures, should be preceeded by the
CALLkeyword.User-defined functions used in COMPUTED COLUMNS will fail during the execution.
Related EWIs¶
SSC-EWI-0067: UDF was transformed to Snowflake procedure, calling procedures inside a query is not supported.
SSC-EWI-0068: User defined function was transformed to a Snowflake procedure.
SSC-EWI-0073: Pending Functional Equivalence Review.
Snowflake Script UDF (SCALAR)¶
Translation reference for SQL Server Scalar User Defined Functions to Snowflake Scripting UDFs
Applies to
SQL Server
Azure Synapse Analytics
Description¶
SnowConvert supports translating SQL Server Scalar User Defined Functions directly to Snowflake Scripting UDFs (SnowScript UDFs) when they meet specific criteria, instead of converting all functions to Stored Procedures.
Snowflake Scripting UDFs are user-defined functions written using Snowflake’s procedural language syntax (Snowscript) within a SQL UDF body. They support variables, loops, conditional logic, and exception handling.
When Functions Become SnowScript UDFs¶
SnowConvert analyzes each SQL Server function and automatically determines the appropriate Snowflake target. A function becomes a SnowScript UDF when it contains only procedural logic without data access operations.
Sample Source Patterns¶
Simple Calculation Function¶
A basic scalar function that performs calculations without querying data.
SQL Server¶
Result¶
Profit |
|---|
50.00 |
Snowflake (SnowScript UDF)¶
Result¶
PROFIT |
|---|
50.00 |
Function with Conditional Logic (IF/ELSE)¶
Functions using IF/ELSE statements for business logic.
SQL Server¶
Result¶
DiscountRate |
|---|
0.20 |
Snowflake (SnowScript UDF)¶
Result¶
DISCOUNTRATE |
|---|
0.20 |
Function with WHILE Loop¶
Functions using WHILE loops for iterative calculations.
SQL Server¶
Result¶
FactorialResult |
|---|
120 |
Snowflake (SnowScript UDF)¶
Result¶
FACTORIALRESULT |
|---|
120 |
String Manipulation Function¶
Complex string operations using loops and conditional logic.
SQL Server¶
Result¶
CleanPhone |
|---|
5551234567 |
Snowflake (SnowScript UDF)¶
Result¶
CLEANPHONE |
|---|
5551234567 |
CASE Statement Logic¶
Functions using CASE expressions for categorization.
SQL Server¶
Result¶
Grade |
|---|
B |
Snowflake (SnowScript UDF)¶
Result¶
GRADE |
|---|
B |
Select Into variable assignment¶
Functions using simple select into for variable assignment.
SQL Server¶
Result¶
CALCULATEPRICE(100, 3) |
|---|
285 |
Snowflake (SnowScript UDF)¶
Result¶
CALCULATEPRICE(100, 3) |
|---|
285 |
Known Issues¶
Warning
SnowConvert AI will not translate UDFs containing the following elements into SnowScripting UDFs, as these features are unsupported in SnowScripting UDFs:
Access database tables
Use cursors
Call other UDFs
Contain aggregate or window functions
Perform DML operations (INSERT/UPDATE/DELETE)
Return result sets
Related EWIs¶
SSC-EWI-0067: UDF was transformed to Snowflake procedure, calling procedures inside a query is not supported.
SSC-EWI-0068: User defined function was transformed to a Snowflake procedure.
SSC-EWI-0073: Pending Functional Equivalence Review.