Snowflake Scripting UDFs¶
Snowflake supports SQL user-defined functions (UDFs) that contain Snowflake Scripting procedural language. These UDFs are called Snowflake Scripting UDFs.
Snowflake Scripting UDFs can be called in a SQL statement, such as a SELECT statement or INSERT statement. Therefore, they are more flexible than a Snowflake Scripting stored procedure, which can only be called in a SQL CALL command.
General usage¶
A Snowflake Scripting UDF evaluates procedural code and returns a scalar (that is, single) value.
You can use the following subset of Snowflake Scripting syntax in Snowflake Scripting UDFs:
Supported data types¶
Snowflake Scripting UDFs support the following data types for both input arguments and return values:
Numeric data types (for example, INTEGER, NUMBER, and FLOAT)
String & binary data types (for example, VARCHAR and BINARY)
Date & time data types (for example, DATE, TIME, and TIMESTAMP)
Logical data types (for example, BOOLEAN)
Snowflake Scripting UDFs support the following data types for input arguments only:
Semi-structured data types (for example, VARIANT, OBJECT, and ARRAY)
Structured data types (for example, ARRAY, OBJECT, and MAP)
Limitations¶
The following limitations apply to Snowflake Scripting UDFs:
The following types of Snowflake Scripting syntax aren’t supported in Snowflake Scripting UDFs:
SQL statements aren’t supported in Snowflake Scripting UDFs (including SELECT, INSERT, UPDATE, and so on).
Snowflake Scripting UDFs can’t be defined as table functions.
The following expression types aren’t supported in Snowflake Scripting UDFs:
User-defined functions
Aggregation functions
Window functions
Snowflake Scripting UDFs can’t be used when creating a materialized view.
Snowflake Scripting UDFs can’t be used when creating row access policies and masking policies.
Snowflake Scripting UDFs can’t be used to specify a default column value.
Snowflake Scripting UDFs can’t be used in a COPY INTO command for data loading and unloading.
Snowflake Scripting UDFs can’t be memoizable.
Snowflake Scripting UDFs have a limit of 500 input arguments.
You can’t log messages for Snowflake Scripting UDFs.
Examples¶
The following examples create and call Snowflake Scripting UDFs:
Create a Snowflake Scripting UDF that returns a value for an INSERT statement
Create a Snowflake Scripting UDF called in WHERE and ORDER BY clauses
Create a Snowflake Scripting UDF with variables¶
Create a Snowflake Scripting UDF that calculates profit based on the values of two arguments:
Note
If you use Snowflake CLI, SnowSQL,
the Classic Console, or the execute_stream or execute_string method in
Python Connector code, this example requires minor
changes. For more information, see Using Snowflake Scripting in Snowflake CLI, SnowSQL, and Python Connector.
Call calculate_profit in a query:
You can use the same Snowflake Scripting UDF and specify columns for the arguments. First, create a table and insert data:
Call calculate_profit in a query and specify the columns for the arguments:
Create a Snowflake Scripting UDF with conditional logic¶
Create a Snowflake Scripting UDF that uses conditional logic to determine the department name based on an input INTEGER value:
Note
If you use Snowflake CLI, SnowSQL,
the Classic Console, or the execute_stream or execute_string method in
Python Connector code, this example requires minor
changes. For more information, see Using Snowflake Scripting in Snowflake CLI, SnowSQL, and Python Connector.
Call check_dept in a query:
You can use a SQL variable in an argument when you
call a Snowflake Scripting UDF. The following example sets a SQL variable and then uses the
variable in a call to the check_dept UDF:
Create a Snowflake Scripting UDF with a loop¶
Create a Snowflake Scripting UDF that uses a loop to count all numbers up to a target number provided in an argument and calculate the sum of all of the numbers counted:
Note
If you use Snowflake CLI, SnowSQL,
the Classic Console, or the execute_stream or execute_string method in
Python Connector code, this example requires minor
changes. For more information, see Using Snowflake Scripting in Snowflake CLI, SnowSQL, and Python Connector.
Call count_to in a query:
Create a Snowflake Scripting UDF with exception handling¶
Create a Snowflake Scripting UDF that declares an exception and then raises the exception:
Note
If you use Snowflake CLI, SnowSQL,
the Classic Console, or the execute_stream or execute_string method in
Python Connector code, this example requires minor
changes. For more information, see Using Snowflake Scripting in Snowflake CLI, SnowSQL, and Python Connector.
Call raise_exception in a query and specify 1 for the input value:
Call raise_exception in a query and specify 2 for the input value:
Call raise_exception in a query and specify NULL for the input value:
Create a Snowflake Scripting UDF that returns a value for an INSERT statement¶
Create a Snowflake Scripting UDF that returns a value that is used in an INSERT statement. Create the table that the values will be inserted into:
Create a SQL UDF that returns a numeric value:
Note
If you use Snowflake CLI, SnowSQL,
the Classic Console, or the execute_stream or execute_string method in
Python Connector code, this example requires minor
changes. For more information, see Using Snowflake Scripting in Snowflake CLI, SnowSQL, and Python Connector.
Call value_to_insert in multiple INSERT statements:
Query the table to view the inserted values:
Create a Snowflake Scripting UDF called in WHERE and ORDER BY clauses¶
Create a Snowflake Scripting UDF that returns a value that is used in a WHERE or ORDER BY clause. Create a table and insert values:
Create a SQL UDF that returns a numeric value that is the product of the multiplication of two input values:
Note
If you use Snowflake CLI, SnowSQL,
the Classic Console, or the execute_stream or execute_string method in
Python Connector code, this example requires minor
changes. For more information, see Using Snowflake Scripting in Snowflake CLI, SnowSQL, and Python Connector.
Call get_product in the WHERE clause of a query to return the rows
where the product is greater than 350:
Call get_product in the ORDER BY clause of a query to order
the results from the lowest to the highest product returned by
the UDF: