IF

Translation reference to convert Transact-SQL IF..ELSE clauses to Snowflake Scripting

Important Notice: Migration of Documentation Website

Please be advised that our documentation website is currently undergoing a migration to a new platform. To ensure you have access to the most up-to-date information, we kindly request that you visit our new documentation website located at:

Official Snowflake Snowconvert Documentation

For any immediate assistance or if you encounter any issues, please contact our support team at [email protected].

Thank you for your understanding.

Applies to

Description

The IF clause allows an SQL statement or a block of statements to be conditionally executed as long as the Boolean expression is true; otherwise, the statements in the optional ELSE clause will be executed. Transact-SQL also supports embedding multiple IF... ELSE clauses in case multiple conditions are required, or the CASE clause can also be used.

For more information for Transact-SQL IF...ELSE, check here.

IF Boolean_expression   
     { sql_statement | statement_block }   
[ ELSE   
     { sql_statement | statement_block } ]   

Note: To define a statement block, use the control-of-flow keywords BEGIN and END.

Sample Source Patterns

Transact-SQL

The following code refers to an IF... ELSE in Transact-SQL that conditions the variable @value to identify if it is less than 5, if it is between 5 and 10, or if it has any other value. Since @value is initialized as 7, the second condition must be true and the result must be 200.

Snowflake Scripting

Notice that in Snowflake Scripting, the embedded IF... ELSE condition is called ELSEIF.

Besides, the Boolean condition is encapsulated in parentheses and the clause always ends with the END IF expression.

In addition, in Snowflake Scripting it is not necessary to use the BEGIN and END keywords to define a statement block, however it can be used if required.

IF statement outside routines (functions and procedures)

Unlike Transact-SQL, Snowflake does not support executing isolated statements like IF...ELSE outside routines like functions or procedures. For this scenario, the statement should be encapsulated in an anonymous block, as shown in the following example. You can read more about how to correctly return the output values in the SELECT section.

Transact-SQL

Snowflake Scripting

Known Issues

No issues were found.

  1. SSC-EWI-0073: Pending Functional Equivalence Review.

  2. SSC-FDM-0020: Multiple result sets are returned in temporary tables.

Last updated