Loop Statements in PL/SQL
In PL/SQL, Loops allow you to execute a sequence of statements multiple times. PL/SQL provides three main types of loop structures to handle different scenarios.
1. Basic Loop (Infinite until Exit)
This is the simplest loop. It repeats indefinitely until it hits an
EXIT or EXIT WHEN statement.LOOP
— code to repeat
EXIT WHEN condition; — Required to avoid infinite loop
END LOOP;
2. WHILE Loop
This loop checks the condition before each iteration. If the condition is false at the start, the code inside is never executed.
WHILE condition LOOP
— code to repeat
END LOOP;
3. FOR Loop
This loop runs for a fixed number of times, automatically incrementing a counter variable.
FOR counter IN 1..10 LOOP
-- code to repeat
END LOOP;