8. SQL Statements : REPEAT - ENDREPEAT : Description
 
Share this page                  
Description
The REPEAT-ENDREPEAT statement defines a program loop. This statement can only be used inside a database procedure.
The Boolean expression (boolean_expr) must evaluate to true or false. A Boolean expression can include comparison operators (=, >, and so on) and these logical operators:
AND
OR
NOT
The statement list can include any series of legal database procedure statements, including another WHILE or REPEAT statement.
The series of statements between REPEAT and UNTIL will always execute once. Then as long as the condition represented by the Boolean expression remains true, the statements will continue to be executed. The condition is tested only at the end of each loop. If values change inside the body of the loop so as to make the condition false, execution continues for the current iteration of the loop, unless an ENDLOOP statement is encountered.
The ENDLOOP statement terminates a REPEAT loop. When ENDLOOP is encountered, the loop is immediately closed, and execution continues with the first statement following ENDREPEAT. For example:
REPEAT
        statement_list_1
        IF condition_1 THEN
                ENDLOOP;
        ENDIF;
        statement_list_2
UNTIL condition_2 ENDWHILE;
In this case, if condition_1 is true, statement_list_2 is not executed in that pass through the loop, and the entire loop is closed. Execution resumes at the statement following the ENDREPEAT statement.
A REPEAT statement can be labeled. The label enables the ENDLOOP statement to break out of a nested series of REPEAT statements to a specified level. The label precedes REPEAT and is specified by a unique alphanumeric identifier followed by a colon, as in the following:
A: REPEAT...
The label must be a legal object name. For details, see the chapter "Introducing SQL." The ENDLOOP statement uses the label to indicate which level of nesting to break out of. If no label is specified after ENDLOOP, only the innermost loop currently active is closed.
The following example illustrates the use of labels in nested REPEAT statements:
label_1;            REPEAT
                     statement_list_1
label_2:                   REPEAT
                             statement_list_2
                             IF condition_1 THEN
                                    ENDLOOP label_1;
                             ELSEIF condition_2 THEN
                                    ENDLOOP label_2;
                             ENDIF;
                             statement_list_3
                     UNTIL condition_3 ENDREPEAT;
                     statement_list_4
             UNTIL condition_4 ENDREPEAT;
In this example, there are two possible breaks out of the inner loop. If condition_1 is true, both loops are closed, and control resumes at the statement following the outer loop. If condition_1 is false but condition_2 is true, the inner loop is exited and control resumes at statement_list_4.
If an error occurs during the evaluation of a REPEAT statement, the database procedure terminates and control returns to the calling application.