LOOP
A simple loop runs until you explicitly end the loop. The syntax for a simple loop is as follows:
LOOP
statements
END LOOP;
To end the loop, you use an EXIT
or an EXIT WHEN
statement.
The EXIT
statement ends a loop immediately;
The EXIT WHEN
statement ends a loop when a specified condition occurs.
The EXIT WHEN
statement can appear anywhere in the loop code.
SQL> DECLARE
2 v_counter INTEGER;
3 BEGIN
4 v_counter := 1;
5 LOOP
6 v_counter := v_counter + 1;
7 DBMS_OUTPUT.PUT_LINE('counter: '||v_counter);
8 EXIT WHEN v_counter = 5;
9 END LOOP;
10 END;
11 /
counter: 2
counter: 3
counter: 4
counter: 5
PL/SQL procedure successfully completed.
SQL>
SQL>
SQL>
SQL>
SQL>