WHILE statement

The WHILE statement repeats the execution of a statement or group of statements while a specified condition is true.

Syntax

Read syntax diagramSkip visual syntax diagramlabel:1WHILEsearch-conditionDO SQL-procedure-statement;END WHILElabel
Notes:
  • 1 Only one label: can be specified for each SQL-procedure-statement. If an ending label is specified for this beginning label, the labels must be the same.

Description

label
Specifies the label for the WHILE statement. If the ending label is specified, it must be the same as the beginning label.

A label name cannot be the same as the name of the SQL procedure in which the label is used.

search-condition
Specifies a condition that is evaluated before each execution of the loop. If the condition is true, the SQL procedure statement in the loop is executed.
SQL-procedure-statement
Specifies the statements to be executed in the loop. The statement must be one of the statements listed under SQL-procedure-statement (external).

Examples

Use a WHILE statement to fetch rows from a table while SQL variable at_end, which indicates whether the end of the table has been reached, is 0.

WHILE at_end = 0 DO
 FETCH c1 INTO
  v_firstnme, v_midinit,
  v_lastname, v_edlevel, v_salary;
 IF SQLCODE=100 THEN SET at_end=1;
 END IF;
END WHILE