Examples for common use cases of Snowflake Scripting¶
You can write anonymous blocks and stored procedures that use Snowflake Scripting language elements, data types, and variables for solutions that address common use cases. This topic includes examples of Snowflake Scripting code for some common use cases.
Update table data with user input¶
The following example creates a stored procedure that updates table data with user input. It uses a FOR loop to iterate over the rows in a RESULTSET for the table. The FOR loop contains conditional logic. Bind variables based on user input determine the exact updates performed by the stored procedure.
The example uses the following data:
The following stored procedure uses a FOR loop to iterate over the rows in a RESULTSET for the bonuses table.
It applies the bonus as the specified percentage of the salary of each employee with the specified performance
rating. The stored procedure uses conditional logic to apply the bonus only to the employees with the specified
performance rating. It also uses the inputs (bonus_percentage and performance_value) as bind variables.
Note: If you use Snowflake CLI, SnowSQL, the Classic Console, or the
execute_stream or execute_string method in Python Connector
code, use this example instead (see Using Snowflake Scripting in Snowflake CLI, SnowSQL, and Python Connector):
To run the stored procedure, specify the bonus percentage and the performance rating. For example, call the stored procedure and apply a 3% bonus for employees with a performance rating of 5:
Run a query to show the results:
Filter and collect data¶
The following example creates a stored procedure that filters and collects the data in a table. The procedure inserts rows using the collected data into another table to track historical trends.
The example uses the following data, which tracks the ownership and settings of virtual machines (VMs):
Assume that a company wants to track the data in this table over time when the values of the settings exceed specific
thresholds. The following stored procedure collects and filters the data in the vm_settings table, then inserts rows into
the vm_settings_history table when the following conditions are met:
A
vm_settingwith a value ofs1is set lower than5.A
vm_settingwith a value ofs2is set higher than500.
The rows inserted into the vm_settings_history table include all of the column values from the vm_settings
table, along with the emp_id of the employee who owns the VM and the current date.
Note: If you use Snowflake CLI, SnowSQL, the Classic Console, or the
execute_stream or execute_string method in Python Connector
code, use this example instead (see Using Snowflake Scripting in Snowflake CLI, SnowSQL, and Python Connector):
Run the stored procedure:
You can see the data that the procedure inserted into the vm_settings_history table by running
the following query: