Cursor For Loop
The following procedure is followed in most of the situations in PL/SQL:
- Open a cursor
- Start a loop
- Fetch the cursor
- Check whether rows are returned
- Close the loop
- Close the cursor
Cursor For Loop allows us to simplify this procedure by letting PL/SQL do most of the things for us.
You can simplify coding by using a Cursor For Loop instead of the OPEN, FETCH, and CLOSE statements.
A Cursor For Loop implicitly declares its loop index as a record that represents a row fetched from the database.
Next, it opens a cursor, repeatedly fetches rows of values from the result set into fields in the record, and then closes the cursor when all rows have been processed.
Cursor For Loop Example
DECLARE CURSOR c1 IS
SELECT ename, sal, deptno
FOR emp_rec IN c1 LOOP
salary_total := salary_total +
Another example of Cursor For Loop
Let us rewrite the example (used in Cursors with Parameters) again using Cursor For Loop