Labeling a PL/SQL Loop
Like PL/SQL blocks, loops can be labeled. The optional label, an undeclared identifier enclosed by double angle brackets, must appear at the beginning of theLOOP
statement. The label name can also appear at the end of the LOOP
statement. When you nest labeled loops, use ending label names to improve readability.With either form of
EXIT
statement, you can complete not only the current loop, but any enclosing loop. Simply label the enclosing loop that you want to complete. Then, use the label in an EXIT
statement, as shown in Example 4-9. Every enclosing loop up to and including the labeled loop is exited.DECLARE s PLS_INTEGER := 0; i PLS_INTEGER := 0; j PLS_INTEGER; BEGIN <> LOOP i := i + 1; j := 0; < > LOOP j := j + 1; s := s + i * j; -- sum a bunch of products EXIT inner_loop WHEN (j > 5); EXIT outer_loop WHEN ((i * j) > 15); END LOOP inner_loop; END LOOP outer_loop; DBMS_OUTPUT.PUT_LINE('The sum of products equals: ' || TO_CHAR(s)); END; /
http://docs.oracle.com/cd/B19306_01/appdev.102/b14261/controlstructures.htm#i8296