WHILE (Snowflake Scripting)¶
A WHILE loop iterates while a specified condition is true.
For more information on loops, see Working with loops.
Note
This Snowflake Scripting construct is valid only within a Snowflake Scripting block.
Syntax¶
Where:
conditionAn expression that evaluates to a BOOLEAN.
labelAn optional label. Such a label can be a jump target for a BREAK or CONTINUE statement. A label must follow the naming rules for Object identifiers.
Usage notes¶
- Put parentheses around the condition in the
WHILE. For example:WHILE ( <condition> ). - If the
conditionnever evaluates to FALSE, and the loop doesn’t contain a BREAK (Snowflake Scripting) command (or equivalent), then the loop will run and consume credits indefinitely. - If the
conditionis NULL, then it is treated as FALSE. - A loop can contain multiple statements. You can use, but are not required to use, a BEGIN … END block to contain those statements.
- Pair the keyword
DOwithEND WHILE, and pair the keywordLOOPwithEND LOOP. For example:
Examples¶
This example uses a loop to calculate a power of 2. The counter variable is the loop counter. The
power_of_2 variable stores the most recent power of 2 that was calculated. (This is an inefficient
solution, but it demonstrates looping.)
Call the stored procedure:
This example uses a loop and the DATEADD function to add a day to a date until the condition is met.
For more examples, see WHILE loop.