Sub topic 14
Choosing the Right Cursor Technique
Start with the simplest operation
This chapter assumes that you know basic SQL, PL/SQL blocks, variables, loops, and exception handling.
Choose a technique based on the work you need to perform:
- Use one SQL statement when the database can perform the whole operation.
- Use a cursor FOR loop when you must perform PL/SQL work for each row.
- Use
BULK COLLECTwhen many rows must cross between SQL and PL/SQL in batches. - Use
SYS_REFCURSORwhen a query must be selected at run time or passed to another program unit. - Use
FOR UPDATEandWHERE CURRENT OFwhen you must lock and update the rows being processed.
A cursor is a handle for a query's result set. Oracle stores the statement's state in a context area. Every cursor technique manages that state either for you or explicitly.
Compare the main choices
| Requirement | Recommended technique | Reason |
|---|---|---|
| Update or delete rows using one rule | Single SQL statement | Usually fastest and simplest |
| Run PL/SQL logic once per row | Cursor FOR loop | Oracle manages open, fetch, and close |
| Process a very large result set in batches | `BULK COLLECT ... LIMIT` | Reduces SQL-to-PL/SQL switches while controlling memory |
| Return a query from a procedure | `SYS_REFCURSOR` | The query can be opened and passed at run time |
| Change the current locked row | `FOR UPDATE` and `WHERE CURRENT OF` | Associates the update with the fetched row |
Prefer set-based SQL, meaning one statement that operates on a set of rows, whenever it expresses the rule clearly. A loop that updates every row separately can be slower and more complicated than one UPDATE.
BEGIN
UPDATE employees
SET salary = salary * 1.05
WHERE department_id = 50;
DBMS_OUTPUT.PUT_LINE(SQL%ROWCOUNT || ' rows updated');
END;
/Use a cursor FOR loop when each row needs procedural logic, such as calling a PL/SQL function, constructing a message, or applying several conditional steps. The loop automatically declares a record and closes the cursor, even when the loop finishes normally.
BEGIN
FOR r IN (
SELECT employee_id, last_name, salary
FROM employees
WHERE department_id = 50
ORDER BY last_name
) LOOP
IF r.salary < 3000 THEN
DBMS_OUTPUT.PUT_LINE(r.last_name || ' needs review');
END IF;
END LOOP;
END;
/Use an explicit cursor only when you need detailed lifecycle control. Examples include opening the cursor at one point, fetching at another, checking cursor attributes, or coordinating a FOR UPDATE operation. If you manually open a cursor, close it on every path where it is no longer needed. An unclosed cursor consumes session resources.
Scale processing deliberately
A cursor FOR loop fetches one row at a time, but its code is usually safer than a manual OPEN, FETCH, and CLOSE sequence. For large result sets, BULK COLLECT fetches several rows per SQL-to-PL/SQL transfer. LIMIT prevents the collection from using unbounded memory.
DECLARE
TYPE t_id_tab IS TABLE OF employees.employee_id%TYPE;
l_ids t_id_tab;
CURSOR c IS SELECT employee_id FROM employees;
BEGIN
OPEN c;
LOOP
FETCH c BULK COLLECT INTO l_ids LIMIT 100;
EXIT WHEN l_ids.COUNT = 0;
FOR i IN 1 .. l_ids.COUNT LOOP
DBMS_OUTPUT.PUT_LINE(l_ids(i));
END LOOP;
END LOOP;
CLOSE c;
END;
/SYS_REFCURSOR is useful at an interface boundary, such as a procedure that returns rows to a caller. It gives flexibility, but the caller must know the columns and data types returned.
A practical decision path
Exercise
You must increase every employee salary in department 50 by five percent, with no per-row procedural rule. Which technique should you choose?
Show answer
Use one set-based UPDATE statement and inspect SQL%ROWCOUNT if you need the number of affected rows.