REPEAT statement

The REPEAT statement executes a statement or group of statements until a search condition is true.

Syntax

Read syntax diagramSkip visual syntax diagramlabel: REPEATSQL-procedure-statement; UNTILsearch-conditionEND REPEATlabel

Description

label
Start of changeSpecifies the label for the REPEAT statement. If an ending label is specified, a matching beginning label must be specified. A label name cannot be the same as the routine name, advanced trigger name, or another label within the same scope. For additional information, see References to SQL labels.End of change
SQL-procedure-statement
Specifies an SQL statement to be executed within the REPEAT loop. The statement must be one of the statements listed under SQL-procedure-statement (SQL PL).
search-condition
Specifies a condition that is evaluated after each execution of the REPEAT loop. If the condition is true, the REPEAT loop will exit. If the condition is unknown or false, the looping continues.

Notes

Considerations for the diagnostics area: At the beginning of the first iteration of the REPEAT statement, and with every subsequent iteration, the diagnostics area is cleared.

Considerations for the SQLSTATE and SQLCODE SQL variables: At the beginning of the first iteration of the REPEAT statement, the SQLSTATE and SQLCODE values reflect the values that were set prior to the REPEAT statement. At the beginning of iterations 2 through n of the REPEAT statement, the SQLSTATE and SQLCODE SQL values reflect the result of evaluating the search condition in the UNTIL clause of that REPEAT. If the loop is terminated with a GOTO, ITERATE, or LEAVE statement, the SQLSTATE and SQLCODE values reflect the successful completion of that statement. Otherwise, after the END REPEAT of the REPEAT statement completes, the SQLSTATE and SQLCODE reflect the result of evaluating the search condition in the UNTIL clause of that REPEAT statement.

Examples

Use a REPEAT statement to fetch rows from a table.

fetch_loop:
REPEAT
 FETCH c1 INTO
  v_firstnme, v_midinit, v_lastname;
UNTIL
    SQLCODE <> 0
END REPEAT fetch_loop