PL/SQL Exit Loop Statement
The PL/SQL LOOP
allows you to iterative sequence of statements repeatedly for a specified number of times. The EXIT WHEN
is used to terminate the loop iteration. Here remember that the EXIT
and EXIT
WHEN
statement can be used interchangeably.
PL/SQL Exit Loop Statement Syntax
To terminate the loop iteration, use the following syntax:
Syntax
LOOP
...
EXIT WHEN condition;
END LOOP;
PL/SQL Exit Loop Statement example with EXIT WHEN Statement
The following PL/SQL uses to terminate
LOOP
using
EXIT WHEN
statement:
Example
DECLARE counter NUMBER := 100;
BEGIN
LOOP
counter := counter + 1;
DBMS_OUTPUT.PUT_LINE(counter);
EXIT WHEN counter = 50;
END LOOP;
END;
Let's understand how it works:
- First, we initialize the counter to 100.
- Second, in each iteration, we increasing counter with 1.
- Third, we display the result of the counter.
- Fourth, The loop is terminated when the counter reaches to 50.