PL/SQL Loop Statement
The PL/SQL Loop
allows you to iterative sequence of statements repeatedly for a specified number of times. It works like any programing language loop works.
PL/SQL Loop Statement Syntax
To iterative sequence of statements repeatedly for a specified number of times, use the following syntax:
Syntax
LOOP
...
EXIT;
END LOOP;
PL/SQL Loop Statement example with EXIT Statement
The following PL/SQL uses
LOOP
with
EXIT
statement to iterative sequence of statements repeatedly for a specified number of times:
Example
DECLARE counter NUMBER := 100;
BEGIN
LOOP
counter := counter + 1;
DBMS_OUTPUT.PUT_LINE(counter);
IF counter = 50 THEN
EXIT;
END IF;
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.