The EXIT statement marks the end of a FOR, WHILE, or FOREACH statement, causing the innermost loop of the specified type (FOR, WHILE, or FOREACH) to terminate. Execution resumes at the first statement outside the loop.
The FOR, WHILE, or FOREACH keyword must immediately follow EXIT. If the database server cannot find the specified loop, the EXIT statement fails. If EXIT is used outside any FOR, WHILE, or FOREACH loop, it generates errors.
The following example uses an EXIT FOR statement. In the FOR loop, when j becomes 6, the IF condition i = 5 in the WHILE loop is true. The FOR loop stops executing, and the SPL procedure continues at the next statement outside the FOR loop (in this case, the END PROCEDURE statement). In this example, the procedure ends when j equals 6:
CREATE PROCEDURE ex_cont_ex() DEFINE i,s,j, INT; FOR j = 1 TO 20 IF j > 10 THEN CONTINUE FOR; END IF LET i,s = j,0; WHILE i > 0 LET i = i -1; IF i = 5 THEN EXIT FOR; END IF END WHILE END FOR END PROCEDURE