WHILE Loop in Oracle PL SQL
We can use WHILE Loop to execute some statements only while a certain condition is met. When it no longer meets the condition, the loop ends.
Every iteration of the loop checks condition to determine if it evaluates
to TRUE. If it does, the action is performed again. If it evaluates to FALSE or NULL,the loop is ended.The WHILE loop executes as long as the stated expression evaluates to TRUE.
Syntax for using WHILE Loop is as follows:
WHILE (condition)
LOOP
–action;
END LOOP;
Every iteration of the loop revisits theĀ condition to determine if it evaluates to TRUE. If it evaluates to TRUE, the action is performed again. If it evaluates to FALSE or NULL,the loop is ended.
An Example Follows
SET SERVEROUTPUT ON;
DECLARE
v_counterĀ INTEGER := 1;
BEGIN
WHILE v_counter <= 10
LOOP
DBMS_OUTPUT.PUT_LINE(‘loop count: ‘||v_counter);
v_counter := v_counter + 1;
END LOOP;
END;
/
Statements between LOOP and END LOOP will be executed while the condition is true.