Control Flow in PL/SQL (Cont.)
At least one of the statements in <loop_body> should be an EXIT statement of the form
EXIT WHEN <condition>;
The LOOP
breaks if <condition> is true.
For example, one way to insert each of the pairs (1, 1) through (10, 10) into T1 of the above two examples is
DECLARE
i NUMBER := 1;
BEGIN
LOOP
INSERT INTO T1 VALUES(i, i);
i := i + 1;
EXIT WHEN i > 10;
END LOOP;
END;
/
|
|
|
|
Some other useful loop-forming statements are:
- EXIT by itself is an unconditional loop break.
Use it inside a conditional if you like.
- A
WHILE
loop can be formed with
WHILE <condition> LOOP
<loop_body>
END LOOP;
A simple FOR
loop can be formed with:
FOR <var> IN <start>..<finish> LOOP
<loop_body>
END LOOP;
Here, <var> can be any variable; it is local to the for-loop and needs not be declared.
Also, <start> and <finish> are constants.