WHILE Statement

Allows SQL statements to be repeated.

Syntax

WHILE condition statement

Syntax Elements

condition
An SQL Search Condition.

statement
A supported SQL statement, or a group of statements enclosed in the BEGIN and END keywords.

Operation

While condition evaluates to true, the WHILE statement executes statement repeatedly.

Example

SET @I = 0
SET @V1 = SECOND(CURTIME())+1
WHILE @I<10
BEGIN
SET @I = @I+1
SET @V2 = @V1*@I + @V1/@I
INSERT INTO TESTDATA VALUES(@I,@V2)
END

Loops, executing the block of three statements for each value of variable @I. The loop terminates when @I becomes equal to 10.

See Also

BREAK statement, CONTINUE statement.