Working with conditional logic¶
Snowflake Scripting supports the following branching constructs for conditional logic:
IF-THEN-ELSEIF-ELSE
CASE
IF statements¶
In Snowflake Scripting, you can execute a set of statements if a condition is met by using an IF statement.
The syntax for the IF statement is:
In an IF statement:
If you need to specify additional conditions, add an ELSEIF clause for each condition.
To specify the statements to execute when none of the conditions evaluate to TRUE, add an ELSE clause.
The ELSEIF and ELSE clauses are optional.
The following is a simple example of an IF statement:
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):
For the full syntax and details about IF statements, see IF (Snowflake Scripting).
For more examples that use the IF statement, see:
Examples for common use cases of Snowflake Scripting - Execute SQL statements based on IF conditions in loops.
BREAK, LOOP, and Working with loops - Execute BREAK statements to terminate a loop based on IF conditions.
EXCEPTION - Raise exceptions based on IF conditions.
CASE statements¶
A CASE statement behaves similarly to an IF statement but provides a simpler way to specify multiple conditions.
Snowflake Scripting supports two forms of the CASE statement:
The next sections explain how to use these different forms.
Note
Snowflake supports other uses of the keyword CASE outside of Snowflake Scripting (e.g. the conditional expression CASE).
Simple CASE statements¶
In a simple CASE statement, you define different branches (WHEN clauses) for different possible values of a given expression.
The syntax for the simple CASE statement is:
Snowflake executes the first branch for which value_n_of_expression matches the value of expression_to_match.
For example, suppose that you want to execute different statements, based on the value of the expression_to_evaluate variable.
For each possible value of this variable (e.g. value a, value b, etc.), you can define a WHEN clause that
specifies the statement(s) to execute:
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):
For the full syntax and details about CASE statements, see CASE (Snowflake Scripting).
Searched CASE statements¶
In the searched CASE statement, you specify different conditions for each branch (WHEN clause). Snowflake executes the first branch for which the expression evaluates to TRUE.
The syntax for the searched CASE statement is:
For example, when you execute the following CASE statement, the returned value is a is x because that branch is the first
branch in which the expression evaluates to TRUE:
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):
For the full syntax and details about CASE statements, see CASE (Snowflake Scripting).