Conditional Looping means executing the same code again and again, as long as the specified condition is true. 'WHILE' statement helps in executing a statement or a block of statement repeatedly, till the specified condition(s) is true. The number of times depends on the condition statement writen after the 'WHILE' keyword.
The execution of statements in the WHILE loop can be controlled from inside the loop with the BREAK and CONTINUE keywords. Break causes an exit from the innermost WHILE loop. Any statements that appear after the END keyword, marking the end of the loop, are executed. CONTINUE causes the WHILE loop to restart, ignoring any statements after the CONTINUE keyword.
Syntax for WHILE
WHILE ( Boolean_expression ) BEGIN
'Sql Statement Block'
END
Simple WHILE Example
WHILE (x>y) BEGIN
SELECT 'If x is greater than y, this statement will be executed'
x = x + 1
END
Nested WHILE Example
WHILE ( x > y ) BEGIN
SELECT 'If x is greater than y and z, this statement will be executed'
x = x + 1
WHILE ( p > q )
BEGIN
SELECT 'If p is greater than q, this statement will be executed'
p = p + 1
END
END
Comments
Post a Comment