WHILE
Translation reference to convert SQL Server While Statement to Snowflake Scripting
Description
The While statement allows an SQL statement or a block of statements to be repeatedly executed as long as the specified condition is true. The execution of statements in the WHILE loop can be controlled from inside the loop with the BREAK and CONTINUE keywords.
For more information for SQL Server While, check here.
WHILE Boolean_expression
{ sql_statement | statement_block | BREAK | CONTINUE }Note: To define a statement block, use the control-of-flow keywords BEGIN and END.
Sample Source Patterns
Basic source pattern code
SQL Server
The following code refers to a While Loop in SQL Server that iterates the @Iteration variable and controls the flow of the loop to terminate when the value of @Iteration equals 10.
CREATE OR ALTER PROCEDURE WhileDemoProcedure
AS
DECLARE @iteration INT;
SET @iteration = 1;
WHILE @iteration < 100
BEGIN
IF @iteration = 10
BREAK;
ELSE
BEGIN
SET @iteration = @iteration + 1;
CONTINUE;
SET @iteration = 2 * @iteration;
END;
END;
RETURN @iteration;
GO
DECLARE @result INT;
EXEC @result = WhileDemoProcedure;
PRINT @result;Snowflake Scripting
Snowflake Scripting allows to use LOOP keyword instead of DO and the END LOOP expression instead of END WHILE .
While with empty body Source Pattern
SQL Server
Snowflake Scripting
This statement can not have an empty body in Snowflake Scripting, to solve this cases a default BREAK statement is added when an empty body is detected.
WHILE statement outside routines (functions and procedures)
Unlike SQL Server, Snowflake does not support executing isolated statements like WHILE outside routines like functions or procedures. For this scenario, the statement should be encapsulated in an anonymous block, as shown in the following example.
SQL Server
Snowflake Scripting
Known Issues
No issues were found.
Related EWIs
SSC-EWI-0073: Pending Functional Equivalence Review.
Last updated