Exit a loop

In a FOR, FOREACH, LOOP, or WHILE loop that has no label, you can use the CONTINUE or EXIT statement to control the execution of the loop.
  • CONTINUE causes the routine to skip the statements in the rest of the loop and move to the next iteration of the FOR, LOOP, or WHILE statement.
  • EXIT ends the loop and causes the routine to continue executing with the first statement following the END FOR, the END LOOP, or the END WHILE keywords.

Remember that EXIT must be followed by the FOREACH keyword when it appears within a FOREACH statement that is the innermost loop of nested loop statements. EXIT can appear with no immediately following keyword when it appears within the FOR, LOOP, or WHILE statement, but an error is issued if you specify a keyword that does not match the loop statement from which the EXIT statement was issued. An error is also issued if EXIT appears outside the context of a loop statement.

For more information about loops in SPL routines, including labelled loops, see HCL OneDB™ Guide to SQL: Syntax.

The following figure shows examples of CONTINUE and EXIT within a FOR loop.
Figure 1: Examples of CONTINUE and EXIT within a FOR loop.
FOR i = 1 TO 10
   IF i = 5 THEN
      CONTINUE FOR;
. . .
   ELIF i = 8 THEN
      EXIT FOR;
   END IF;

END FOR;
Tip: You can use CONTINUE and EXIT to improve the performance of SPL routines so that loops do not execute unnecessarily.