Translation reference to convert SQL Server UDF (User Defined Functions) with scalar return type to Snowflake.
Description
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 Reference CREATE FUNCTION subsection).
These functions are usually used inside the SELECTstatement, or single variable setup (most likely inside a stored procedure).
SQL Server Syntax
-- Transact-SQL Scalar Function SyntaxCREATE [ OR ALTER ] FUNCTION [ schema_name. ] function_name( [ { @parameter_name [ AS ][ type_schema_name. ] parameter_data_type [ = default ] [ READONLY ] } [ ,...n ] ])RETURNS return_data_type [ WITH <function_option> [ ,...n ] ] [ AS ] BEGIN function_body RETURN scalar_expression END[ ; ]
Snowflake Syntax
Snowflake allows 3 different languages in their user defined functions:
SQL
JavaScript
Java
For now, SnowConvert will support only SQL and JavaScript as target languages.
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 user defined functions allows multiple statements in their bodies, but cannot perform queries to the database. (Scalar JavaScript UDFs Reference)
CREATE [ OR REPLACE ] [ SECURE ] FUNCTION <name> ( [ <arg_name> <arg_data_type> ] [ , ... ] ) RETURNS { <result_data_type> | TABLE ( <col_name><col_data_type> [ , ... ] ) } [ [ NOT ] NULL ] LANGUAGE JAVASCRIPT [ { CALLED ON NULL INPUT | { RETURNS NULL ON NULL INPUT | STRICT } } ] [ VOLATILE | IMMUTABLE ] [ COMMENT = '<string_literal>' ]AS'<function_definition>'
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.
SQL Server
IN -> SqlServer_01.sql
CREATE OR ALTER FUNCTION PURCHASING.GetVendorName()RETURNS NVARCHAR(50) ASBEGIN DECLARE @result NVARCHAR(50) DECLARE @BUSINESSENTITYID INT SET @BUSINESSENTITYID =1492 SELECT @result =Name FROM PURCHASING.VENDOR WHERE BUSINESSENTITYID = @BUSINESSENTITYID RETURN @resultENDGOSELECT PURCHASING.GetVendorName() as vendor_name;
vendor_name |
-----------------------+
Australia Bike Retailer|
Snowflake
OUT -> SqlServer_01.sql
CREATE OR REPLACE FUNCTION PURCHASING.GetVendorName ()RETURNS VARCHAR(50)LANGUAGE SQLCOMMENT ='{"origin":"sf_sc","name":"snowconvert","version":{"major":1, "minor":0},"attributes":{"component":"transact"}}'AS$$ WITH CTE1 AS ( SELECT1492AS BUSINESSENTITYID ), CTE2 AS ( SELECTNameAS RESULT FROM PURCHASING.VENDOR WHERE BUSINESSENTITYID = ( SELECT BUSINESSENTITYID FROM CTE1 ) ) SELECT RESULT FROM CTE2$$;SELECT PURCHASING.GetVendorName() as vendor_name;
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.
SQL Server
IN -> SqlServer_02.sql
CREATE OR ALTER FUNCTION PURCHASING.HasActiveFlag(@BusinessEntityID int)RETURNS VARCHAR(10) ASBEGIN DECLARE @result VARCHAR(10) DECLARE @ActiveFlag BIT SELECT @ActiveFlag = ActiveFlag from PURCHASING.VENDOR v where v.BUSINESSENTITYID = @BusinessEntityID IF @ActiveFlag =1 SET @result ='YES' ELSE IF @ActiveFlag =0 SET @result ='NO' RETURN @resultENDGOSELECT PURCHASING.HasActiveFlag(1516) as has_active_flag;
has_active_flag|
---------------+
NO |
Snowflake
OUT -> SqlServer_02.sql
CREATE OR REPLACE FUNCTION PURCHASING.HasActiveFlag (P_BUSINESSENTITYID INT)RETURNS VARCHAR(10)LANGUAGE SQLCOMMENT ='{"origin":"sf_sc","name":"snowconvert","version":{"major":1, "minor":0},"attributes":{"component":"transact"}}'AS$$ WITH CTE1 AS ( SELECT ActiveFlag AS ACTIVEFLAGfrom PURCHASING.VENDOR vwhere v.BUSINESSENTITYID = P_BUSINESSENTITYID ), CTE2 AS ( SELECT CASE WHEN ( SELECT ACTIVEFLAG FROM CTE1 ) =1 THEN 'YES' WHEN ( SELECT ACTIVEFLAG FROM CTE1 ) =0 THEN 'NO' END AS RESULT ) SELECT RESULT FROM CTE2$$;SELECT PURCHASING.HasActiveFlag(1516) as has_active_flag;
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 in order to preserve the execution order.
CASE EXPRESSIONS only can return one value per statement
Example
The following code in both programming paradigms is functionally equivalent.
DECLARE @VendorId ASint;DECLARE @AccountNumber ASVARCHAR(50);SELECT @VendorId = poh.VendorID FROM Purchasing.PurchaseOrderHeader poh WHERE PurchaseOrderID =1SELECT @AccountNumber = v.AccountNumber FROM Purchasing.Vendor v WHERE v.BusinessEntityID = @VendorId
SELECT V.AccountNumber AccountNumberFROM (SELECT poh.VendorID VendorId FROM Purchasing.PurchaseOrderHeader poh WHERE PurchaseOrderID =1) T1, Purchasing.Vendor vWHERE v.BusinessEntityID = T1.VendorId
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
IN -> SqlServer_03.sql
CREATE or ALTER FUNCTION PURCHASING.SELECTINUDF ( @param1 varchar(12))RETURNS intASBEGINdeclare @var1 int;declare @var2 int;declare @var3 int; IF @param1 ='first' BEGINselect @var1 = col1 +10from table1 WHERE id =0;select @var2 = col1 +20from table1 WHERE id =0;select @var3 = col1 +30from table1 WHERE id =0; END IF @param1 ='second' BEGINdeclare @var4 int=10;select @var1 = col1 +40from table1 WHERE id =0;select @var2 = col1 +40from table1 WHERE id =0; END IF @param1 ='third' BEGINselect col1 from table1 where id =0;select @var1 = col1 +50from table1 WHERE id =0;select @var2 = col1 +50from table1 WHERE id =0; END RETURN @var1ENDSELECT PURCHASING.SELECTINUDF('first') as result; -- Assuming table1.col1 is 0 when ID = 0
RESULT|
------+
10|
Snowflake
OUT -> SqlServer_03.sql
CREATE OR REPLACE FUNCTION PURCHASING.SELECTINUDF (PARAM1 STRING)RETURNS INTLANGUAGE SQLCOMMENT ='{"origin":"sf_sc","name":"snowconvert","version":{"major":1, "minor":0},"attributes":{"component":"transact"}}'AS$$ WITH CTE1 AS ( SELECT CASE WHEN PARAM1 ='first' THEN (SELECT col1 +10AS VAR1 from table1 WHERE id =0) END AS VAR1, CASE WHEN PARAM1 ='first' THEN (SELECT col1 +20AS VAR2 from table1 WHERE id =0) END AS VAR2, CASE WHEN PARAM1 ='first' THEN (SELECT col1 +30AS VAR3 from table1 WHERE id =0) END AS VAR3 ), !!!RESOLVE EWI!!! /*** SSC-EWI-0073 - PENDING FUNCTIONAL EQUIVALENCE REVIEW FOR 'IF STATEMENT' NODE ***/!!! CTE2 AS (/* IF @param1 = 'second' BEGIN declare @var4 int = 10; select @var1 = col1 + 40 from table1 WHERE id = 0; select @var2 = col1 + 40 from table1 WHERE id = 0; END*/ SELECTnull ), !!!RESOLVE EWI!!! /*** SSC-EWI-0073 - PENDING FUNCTIONAL EQUIVALENCE REVIEW FOR 'IF STATEMENT' NODE ***/!!! CTE3 AS (/* IF @param1 = 'third' BEGIN select col1 from table1 where id = 0; select @var1 = col1 + 50 from table1 WHERE id = 0; select @var2 = col1 + 50 from table1 WHERE id = 0; END*/ SELECTnull ), CTE4 AS ( SELECT PURCHASING.SELECTINUDF('first') as result ) SELECT VAR1 FROM CTE4$$ -- Assuming table1.col1 is 0 when ID = 0;
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 in order to keep the original behavior.
SQL Server
IN -> SqlServer_04.sql
CREATE OR ALTER FUNCTION Purchasing.GetTotalFreight()RETURNS MONEY ASBEGIN DECLARE @Result MONEY SELECT @Result = ISNULL(SUM(t.Freight), 0) from Purchasing.PurchaseOrderHeader treturn @ResultENDGOselect Purchasing.GetTotalFreight() as Result;
Result |
------------+
1583978.2263|
Snowflake
OUT -> SqlServer_04.sql
CREATE OR REPLACE FUNCTION Purchasing.GetTotalFreight ()RETURNS NUMBER(38, 4)LANGUAGE SQLCOMMENT ='{"origin":"sf_sc","name":"snowconvert","version":{"major":1, "minor":0},"attributes":{"component":"transact"}}'AS$$ WITH CTE1 AS ( SELECT NVL(SUM(t.Freight), 0) AS RESULT from Purchasing.PurchaseOrderHeader t ) SELECT RESULT FROM CTE1$$;select Purchasing.GetTotalFreight() as 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
IN -> SqlServer_05.sql
CREATE OR ALTER FUNCTION PURCHASING.Foo( @PARAM1 INT)RETURNS varchar(25)ASBEGIN DECLARE @filter INT = @PARAM1 DECLARE @NAME VARCHAR(25) = (SELECT Namefrom Purchasing.Vendor v where BusinessEntityID = @filter) SET @NAME = REPLACE(@NAME, 'Australia', 'USA') SET @NAME = REPLACE(@NAME, 'Bike', 'Car') RETURN @NAMEENDGOSELECT PURCHASING.Foo(1492) ASName;
Name |
----------------+
USA Car Retailer|
Snowflake
OUT -> SqlServer_05.sql
CREATE OR REPLACE FUNCTION PURCHASING.Foo (PARAM1 INT)RETURNS VARCHAR(25)LANGUAGE SQLCOMMENT ='{"origin":"sf_sc","name":"snowconvert","version":{"major":1, "minor":0},"attributes":{"component":"transact"}}'AS$$ WITH CTE1 AS ( SELECT PARAM1 AS FILTER ), CTE2 AS ( SELECT (SELECTNamefrom Purchasing.Vendor vwhere BusinessEntityID = ( SELECT FILTER FROM CTE1 ) ) AS NAME ), CTE3 AS ( SELECT REPLACE(( SELECT NAME FROM CTE3 ), 'Australia', 'USA') AS NAME ), CTE4 AS ( SELECT REPLACE(( SELECT NAME FROM CTE4 ), 'Bike', 'Car') AS NAME ) SELECT NAME FROM CTE4$$;SELECT PURCHASING.Foo(1492) ASName;
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
IN -> SqlServer_06.sql
CREATE OR ALTER FUNCTION PURCHASING.FOO() RETURNS MONEYASBEGINdeclare @firstValue MONEYdeclare @secondValue MONEYdeclare @Result MONEYselect @Result =0select @firstValue = SubTotal from Purchasing.PurchaseOrderHeader where PurchaseOrderID =1select @secondValue = SubTotal from Purchasing.PurchaseOrderHeader where PurchaseOrderID =2if @firstValue is not nullselect @Result = @Result + @firstValueif @secondValue is not nullselect @Result = @Result + @secondValuereturn @Result ENDGOSELECT PURCHASING.Foo() AS Result;
Result |
--------+
473.1415|
Snowflake
OUT -> SqlServer_06.sql
CREATE OR REPLACE FUNCTION PURCHASING.FOO ()RETURNS NUMBER(38, 4)LANGUAGE SQLCOMMENT ='{"origin":"sf_sc","name":"snowconvert","version":{"major":1, "minor":0},"attributes":{"component":"transact"}}'AS$$ WITH CTE1 AS (select0AS RESULT ), CTE2 AS (select SubTotal AS FIRSTVALUEfrom Purchasing.PurchaseOrderHeaderwhere PurchaseOrderID =1 ), CTE3 AS (select SubTotal AS SECONDVALUEfrom Purchasing.PurchaseOrderHeaderwhere PurchaseOrderID =2 ), CTE4 AS ( SELECT CASE WHEN ( SELECT FIRSTVALUE FROM CTE2 ) is not null THEN (select ( SELECT RESULT FROM CTE1 ) + ( SELECT FIRSTVALUE FROM CTE2 ) AS RESULT) END AS RESULT ), CTE5 AS ( SELECT CASE WHEN ( SELECT SECONDVALUE FROM CTE3 ) is not null THEN (select ( SELECT RESULT FROM CTE1 ) + ( SELECT SECONDVALUE FROM CTE3 ) AS RESULT) ELSE (SELECT RESULT FROM CTE4) END AS RESULT ) SELECT RESULT FROM CTE5$$;SELECT PURCHASING.Foo() AS 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
IN -> SqlServer_07.sql
CREATE OR ALTER FUNCTION [PURCHASING].[FOO] ()RETURNS INTASBEGIN IF exists (SELECT PreferredVendorStatus FROM Purchasing.Vendor v ) RETURN 1 RETURN 0ENDGOSELECT PURCHASING.FOO() as result;
result|
------+
1|
Snowflake
OUT -> SqlServer_07.sql
CREATE OR REPLACE FUNCTION PURCHASING.FOO ()RETURNS INTLANGUAGE SQLCOMMENT ='{"origin":"sf_sc","name":"snowconvert","version":{"major":1, "minor":0},"attributes":{"component":"transact"}}'AS$$ SELECT CASE WHEN exists (SELECT PreferredVendorStatus FROM Purchasing.Vendor v ) THEN 1 ELSE 0 END$$;SELECT PURCHASING.FOO() as 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 is able to identify first all the original COMMON TABLE EXPRESSION names in order to avoid generating duplicated names.
SQL Server
IN -> SqlServer_08.sql
CREATE OR ALTER FUNCTION [PURCHASING].[FOO] ( @status INT) Returns INTAsBeginDeclare @result asint=0 ;WITH ctetable(RevisionNumber) as ( SELECT RevisionNumber FROM Purchasing.PurchaseOrderHeader pohwhere poh.Status = @status ), finalCte As ( SELECT RevisionNumber FROM ctetable )Select @result =count(RevisionNumber) from finalCte return @result;EndGOSELECT PURCHASING.FOO(4) as result;
result|
------+
3689|
Snowflake
OUT -> SqlServer_08.sql
CREATE OR REPLACE FUNCTION PURCHASING.FOO (STATUS INT)Returns INTLANGUAGE SQLCOMMENT ='{"origin":"sf_sc","name":"snowconvert","version":{"major":1, "minor":0},"attributes":{"component":"transact"}}'AS$$ WITH CTE1 AS ( SELECT0AS RESULT ), ctetable ( RevisionNumber ) as ( SELECT RevisionNumber FROM Purchasing.PurchaseOrderHeader pohwhere poh.Status = STATUS ), finalCte As ( SELECT RevisionNumber FROM ctetable ), CTE2 AS (Select COUNT(RevisionNumber) AS RESULT from finalCte ) SELECT RESULT FROM CTE2$$;SELECT PURCHASING.FOO(4) as 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
IN -> SqlServer_09.sql
CREATE OR ALTER FUNCTION PURCHASING.GetFiscalYear ( @DATE AS DATETIME)RETURNS INTASBEGIN DECLARE @FiscalYear AS INT DECLARE @CurMonth AS INT SET @CurMonth = DATEPART(M,@DATE) SET @FiscalYear = DATEPART(YYYY, @DATE) IF (@CurMonth >=7) BEGIN SET @FiscalYear = @FiscalYear +1 END RETURN @FiscalYearENDGOSELECT PURCHASING.GetFiscalYear('2020-10-10') as DATE;
IN -> SqlServer_10.sql
CREATE OR ALTER FUNCTION PURCHASING.[getCleanChargeCode]( @ChargeCode varchar(50))returnsvarchar(50) asbegindeclare @CleanChargeCode varchar(50),@Len int,@Pos int=2set @Pos=LEN(@ChargeCode)-1while @Pos >1beginset @CleanChargeCode=RIGHT(@ChargeCode,@Pos)ifTRY_CAST(@CleanChargeCode asbigint) is not nullreturn @CleanChargeCodeset @Pos=@Pos-1endset @Pos=LEN(@ChargeCode)-1while @Pos >1beginset @CleanChargeCode=LEFT(@ChargeCode,@Pos)ifTRY_CAST(@CleanChargeCode asbigint) is not nullreturn @CleanChargeCodeset @Pos=@Pos-1endreturnnullendGOSELECT PURCHASING.[getCleanChargeCode]('16test') AS CleanChargeCode;
DATE|
----+
2021|
CleanChargeCode|
---------------+
16 |
Snowflake
OUT -> SqlServer_09.sql
--** SSC-FDM-0029 - USER DEFINED FUNCTION WAS TRANSFORMED TO SNOWFLAKE PROCEDURE **CREATE OR REPLACE PROCEDURE PURCHASING.GetFiscalYear (DATE TIMESTAMP_NTZ(3))RETURNS VARCHARLANGUAGE SQLCOMMENT ='{"origin":"sf_sc","name":"snowconvert","version":{"major":1, "minor":0},"attributes":{"component":"transact"}}'EXECUTE AS CALLERAS$$ DECLARE FISCALYEAR INT; CURMONTH INT; BEGIN CURMONTH := DATE_PART(month, :DATE :: TIMESTAMP); FISCALYEAR := DATE_PART(year, :DATE :: TIMESTAMP); IF ((:CURMONTH >=7)) THEN BEGIN FISCALYEAR := :FISCALYEAR +1; END; END IF; RETURN :FISCALYEAR; END;$$;SELECT !!!RESOLVE EWI!!! /*** SSC-EWI-0073 - PENDING FUNCTIONAL EQUIVALENCE REVIEW FOR 'GetFiscalYear' NODE ***/!!! PURCHASING.GetFiscalYear('2020-10-10') as DATE;
OUT -> SqlServer_10.sql
--** SSC-FDM-0029 - USER DEFINED FUNCTION WAS TRANSFORMED TO SNOWFLAKE PROCEDURE **CREATE OR REPLACE PROCEDURE PURCHASING.getCleanChargeCode (CHARGECODE STRING)RETURNS VARCHARLANGUAGE SQLCOMMENT ='{"origin":"sf_sc","name":"snowconvert","version":{"major":1, "minor":0},"attributes":{"component":"transact"}}'EXECUTE AS CALLERAS$$ DECLARE CLEANCHARGECODE VARCHAR(50); LEN INT; POS INT :=2; BEGIN POS := LEN(:CHARGECODE)-1; WHILE (:POS >1) LOOP CLEANCHARGECODE := RIGHT(:CHARGECODE, :POS); IF (TRY_CAST(:CLEANCHARGECODE as BIGINT) is not null) THEN RETURN :CLEANCHARGECODE; END IF; POS := :POS -1; END LOOP; POS := LEN(:CHARGECODE)-1; WHILE (:POS >1) LOOP CLEANCHARGECODE := LEFT(:CHARGECODE, :POS); IF (TRY_CAST(:CLEANCHARGECODE as BIGINT) is not null) THEN RETURN :CLEANCHARGECODE; END IF; POS := :POS -1; END LOOP; RETURN null; END;$$;SELECT PURCHASING.getCleanChargeCode('16test') !!!RESOLVE EWI!!! /*** SSC-EWI-0067 - UDF WAS TRANSFORMED TO SNOWFLAKE PROCEDURE, CALLING PROCEDURES INSIDE QUERIES IS NOT SUPPORTED ***/!!! AS CleanChargeCode;
DATE |
------+
2021.0|
CLEANCHARGECODE|
---------------+
16 |
Known Issues
User-defined functions 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 DECLARE, OPEN, FETCH, CLOSE or DEALLOCATE a CURSOR. Use a Stored Procedure if you need to use cursors.
User-defined functions cannot perform control-of-flow statements such as WHILE if there is at least one call to the database
User-defined functions with references to other user-defined functions that were transformed to Stored Procedures, will be transformed to Stored Procedures too.
User-defined functions that use @@ROWCOUNT are not supported in SQL and should be transformed to stored procedures in order to keep the functional equivalence.
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
IN -> SqlServer_11.sql
CREATE OR ALTER FUNCTION PURCHASING.FOO()RETURNS INTASBEGIN DECLARE @i int=0, @p int;Select @p = COUNT(*) FROM PURCHASING.VENDOR WHILE (@p <1000) BEGIN SET @i = @i +1 SET @p = @p + @i END IF (@i =6) RETURN 1 RETURN @pENDGOSELECT PURCHASING.FOO() as result;
result|
------+
1007|
Snowflake
OUT -> SqlServer_11.sql
--** SSC-FDM-0029 - USER DEFINED FUNCTION WAS TRANSFORMED TO SNOWFLAKE PROCEDURE **CREATE OR REPLACE PROCEDURE PURCHASING.FOO ()RETURNS VARCHARLANGUAGE SQLCOMMENT ='{"origin":"sf_sc","name":"snowconvert","version":{"major":1, "minor":0},"attributes":{"component":"transact"}}'EXECUTE AS CALLERAS$$ DECLARE I INT :=0; P INT; BEGINSelect COUNT(*) INTO :P FROM PURCHASING.VENDOR; WHILE (:P <1000) LOOP I := :I +1; P := :P + :I; END LOOP; IF ((:I =6)) THEN RETURN 1; END IF; RETURN :P; END;$$;SELECT PURCHASING.FOO() !!!RESOLVE EWI!!! /*** SSC-EWI-0067 - UDF WAS TRANSFORMED TO SNOWFLAKE PROCEDURE, CALLING PROCEDURES INSIDE QUERIES IS NOT SUPPORTED ***/!!! as 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
IN -> SqlServer_12.sql
CREATE OR ALTER FUNCTION PURCHASING.FOO()RETURNS VARCHAR(8000)ASBEGIN DECLARE @names varchar(8000) SET @names ='' SELECT @names = ISNULL(@names +' ', '') +Namefrom Purchasing.Vendor v return @names ENDGOselect PURCHASING.FOO() as names;
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
OUT -> SqlServer_12.sql
--** SSC-FDM-0029 - USER DEFINED FUNCTION WAS TRANSFORMED TO SNOWFLAKE PROCEDURE **CREATE OR REPLACE PROCEDURE PURCHASING.FOO ()RETURNS VARCHARLANGUAGE SQLCOMMENT ='{"origin":"sf_sc","name":"snowconvert","version":{"major":1, "minor":0},"attributes":{"component":"transact"}}'EXECUTE AS CALLERAS$$ DECLARE NAMES VARCHAR(8000); BEGIN NAMES :=''; SELECT NVL(:NAMES ||' ', 0) +Name INTO :NAMESfrom Purchasing.Vendor v; RETURN :NAMES; END;$$;select PURCHASING.FOO() !!!RESOLVE EWI!!! /*** SSC-EWI-0067 - UDF WAS TRANSFORMED TO SNOWFLAKE PROCEDURE, CALLING PROCEDURES INSIDE QUERIES IS NOT SUPPORTED ***/!!! as names;
For the described scenarios above, consider the following limitations:
All the calls to user-defined functions in DML queries such as SELECT, INSERT, DELETE, UPDATE or MERGE will fail because calls to Stored Procedures within these queries are not allowed.
Calls to user-defined functions inside procedures, should be preceeded by the CALL keyword.
Use- 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.