WHILE Construct
Sets a condition for the repeated execution of an sql_statement or statement block. The statements are executed repeatedly 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.
Syntax
WHILE Boolean_expression
{sql_statement | statement_block}
[BREAK]
{sql_statement | statement_block}
[CONTINUE]
where
-
Boolean_expression
-
Is an expression that returns true or false. If the Boolean expression contains a SELECT statement, the SELECT statement must be enclosed in parentheses.
-
sql_statement | statement_block
-
Is any SQL statement or statement grouping as defined with a statement block. To define a statement block, use the control-of-flow keywords BEGIN and END.
-
BREAK
-
Causes an exit from the WHILE loop. BREAK is often (but not always) activated by an IF test. Any statements that appear after the END keyword that marks the end of the loop are executed.
-
CONTINUE
-
Causes the WHILE loop to restart, skipping any statements after CONTINUE. CONTINUE is often (but not always) activated by an IF test.
Remarks
If two or more WHILE loops are nested, the inner BREAK exits to the next outermost loop. First, all the statements after the end of the inner loop run, and then the next outermost loop restarts.