The syntax for the FOR Loop is:
FOR loop_counter IN [REVERSE] lowest_number..highest_number
LOOP
{.statements.}
END LOOP;
You would use a FOR Loop when you want to execute the loop body a fixed number of times.
Let's take a look at an example.
FOR Lcntr IN 1..20
LOOP
LCalc := Lcntr * 31;
END LOOP;
This example will loop 20 times. The counter will start at 1 and end at 20.
The FOR Loop can also loop in reverse. For example:
FOR Lcntr IN REVERSE 1..15
LOOP
LCalc := Lcntr * 31;
END LOOP;
This example will loop 15 times. The counter will start at 15 and end at 1. (loops backwards);
and there is another use:
DECLARE
BEGIN
FOR eno IN (SELECT empno AS id FROM emp WHERE emp.empno IN (7900, 7902))
LOOP
INSERT INTO new_emp
(employee_id,
NAME)
SELECT t.empno,
t.ename
FROM emp t
WHERE t.empno = eno.id;
COMMIT;
END LOOP;
END;