602SQL Documentation Index  

WHILE Statement (SQL)

statement_WHILE ::= [ label : ] WHILE condition DO statement … END WHILE [ label ];

The condition is evaluated repeatedly and the statements (or block statements) enclosed in the condition are executed when the WHILE statement is running. The statement execution is terminated when the condition does not equal TRUE, the LEAVE statement is executed or an error occurs.

If the ending label is set, then the same beginning label must be set as well. No statement may be designated with the same label inside the WHILE statement.

Example:

Insert 10 records into a table and enumerate them ascending.

BEGIN
  DECLARE i INT; 
  SET i = 1;
  WHILE i <= 10 DO 
    INSERT INTO Tab3 (number) VALUES (i); 
    SET i = i + 1; 
  END WHILE; 
END ;