Sub topic 6
Fetch Loop Patterns in PL/SQL
Prerequisites and the fetch cycle
This chapter assumes you know basic SQL and the structure of an anonymous PL/SQL block. You should also understand variables, loops, and basic exception handling.
An explicit cursor is a named query result that PL/SQL reads one row at a time. Each FETCH moves the cursor forward and copies the next row into variables or a record. After a fetch, cursor attributes describe what happened:
%FOUNDisTRUEwhen the fetch returned a row.%NOTFOUNDisTRUEwhen the fetch returned no row.%ROWCOUNTis the number of rows fetched so far.
Immediately after OPEN, %FOUND and %NOTFOUND are NULL. Therefore, the first FETCH must happen before you use these attributes to decide whether to process a row.
The `LOOP` and `EXIT WHEN` pattern
The most common explicit-cursor pattern fetches inside an unconditional loop. It exits immediately after a fetch finds no row.
DECLARE
CURSOR c_emp IS
SELECT employee_id, last_name, salary
FROM employees
WHERE department_id = 50
ORDER BY last_name;
r_emp c_emp%ROWTYPE;
BEGIN
OPEN c_emp;
LOOP
FETCH c_emp INTO r_emp;
EXIT WHEN c_emp%NOTFOUND;
DBMS_OUTPUT.PUT_LINE(
r_emp.employee_id || ': ' ||
r_emp.last_name || ': ' ||
r_emp.salary
);
END LOOP;
CLOSE c_emp;
END;
/The order is important:
- Fetch the next row.
- Test
%NOTFOUND. - Process the record only when a row was fetched.
If you process r_emp before the exit test, the final iteration can process stale values from the previous row. A cursor can return zero rows, so this pattern must also work when the first fetch immediately sets %NOTFOUND to TRUE.
The `WHILE` pattern
A WHILE loop can express the same logic, but it needs an initial fetch before the loop condition. The loop then fetches the next row at the end of each iteration.
DECLARE
CURSOR c_emp IS
SELECT last_name, salary
FROM employees
WHERE department_id = 50
ORDER BY last_name;
r_emp c_emp%ROWTYPE;
BEGIN
OPEN c_emp;
FETCH c_emp INTO r_emp;
WHILE c_emp%FOUND LOOP
DBMS_OUTPUT.PUT_LINE(r_emp.last_name || ': ' || r_emp.salary);
FETCH c_emp INTO r_emp;
END LOOP;
CLOSE c_emp;
END;
/This version is valid because %FOUND is checked only after a fetch. If the query returns no rows, the initial fetch makes %FOUND false and the body runs zero times.
| Pattern | First fetch | End-of-row test | Typical risk |
|---|---|---|---|
| `LOOP` with `EXIT WHEN` | Inside the loop | Before processing | Processing before the exit test |
| `WHILE` with `%FOUND` | Before the loop | Loop condition and final fetch | Forgetting the final fetch |
Choosing a pattern
Prefer LOOP with EXIT WHEN c_name%NOTFOUND when you want the fetch and termination test next to each other. It is usually easier to review and less likely to create an infinite loop.
Use WHILE when the loop condition naturally describes whether processing should continue. In either pattern, keep the cursor lifecycle clear: open it, fetch rows, close it.
Exercise
In a LOOP with EXIT WHEN pattern, where should EXIT WHEN c_emp%NOTFOUND appear?
Show answer
Immediately after FETCH and before processing the fetched variables or record.