Sub topic 3
Cursors with Parameters
This chapter assumes you know basic SQL, the structure of an anonymous PL/SQL block, and explicit cursors with OPEN, FETCH and CLOSE.
Cursors with parameters
A parameterized cursor accepts input values when it is opened, so one cursor definition can serve many filters.
DECLARE
CURSOR c_emp (p_dept NUMBER, p_min_sal NUMBER DEFAULT 0) IS
SELECT last_name, salary
FROM employees
WHERE department_id = p_dept
AND salary >= p_min_sal;
BEGIN
FOR r IN c_emp(50, 3000) LOOP
DBMS_OUTPUT.PUT_LINE(r.last_name || ': ' || r.salary);
END LOOP;
END;
/Locking and updating rows with FOR UPDATE and WHERE CURRENT OF
FOR UPDATE locks the selected rows so no other session changes them before you do. NOWAIT raises an error instead of waiting for a held lock. WHERE CURRENT OF updates the row the cursor is positioned on.
DECLARE
CURSOR c_emp IS
SELECT employee_id, salary
FROM employees
WHERE department_id = 50
FOR UPDATE OF salary NOWAIT;
BEGIN
FOR r IN c_emp LOOP
UPDATE employees
SET salary = r.salary * 1.05
WHERE CURRENT OF c_emp;
END LOOP;
COMMIT;
END;
/REF CURSOR and SYS_REFCURSOR
A SYS_REFCURSOR is a cursor variable. Unlike a named cursor, its query is attached at run time with OPEN ... FOR, and you can pass it to other program units.
DECLARE
l_rc SYS_REFCURSOR;
l_name employees.last_name%TYPE;
l_sal employees.salary%TYPE;
BEGIN
OPEN l_rc FOR
SELECT last_name, salary
FROM employees
WHERE department_id = 50;
LOOP
FETCH l_rc INTO l_name, l_sal;
EXIT WHEN l_rc%NOTFOUND;
DBMS_OUTPUT.PUT_LINE(l_name || ': ' || l_sal);
END LOOP;
CLOSE l_rc;
END;
/Batch fetching with BULK COLLECT and LIMIT
BULK COLLECT fetches many rows in one round trip into a collection. LIMIT caps each batch. Testing COUNT = 0 ends the loop after the last batch.
DECLARE
CURSOR c_emp IS
SELECT employee_id, salary FROM employees;
TYPE t_emp_tab IS TABLE OF c_emp%ROWTYPE;
l_emps t_emp_tab;
BEGIN
OPEN c_emp;
LOOP
FETCH c_emp BULK COLLECT INTO l_emps LIMIT 100;
EXIT WHEN l_emps.COUNT = 0;
FOR i IN 1 .. l_emps.COUNT LOOP
DBMS_OUTPUT.PUT_LINE(l_emps(i).employee_id || ': ' || l_emps(i).salary);
END LOOP;
END LOOP;
CLOSE c_emp;
END;
/Common cursor errors and closing cursors
- ORA-06511 (cursor already open): you called OPEN on a cursor that is still open. Close it first, or open it once.
- ORA-01001 (invalid cursor): you fetched from or closed a cursor that was never opened.
- ORA-01002 (fetch out of sequence): you fetched after the cursor was closed or invalidated.
- ORA-01000 (maximum open cursors exceeded): cursors are left open in a loop. Close each explicit cursor when you finish.
A cursor FOR loop closes its cursor automatically, even when the loop exits early, so it avoids most leaks.
Choosing the right technique
- Use a single SQL statement when it can do the whole job.
- Use a cursor FOR loop when each row needs procedural logic.
- Use
BULK COLLECTwithLIMITwhen row counts are large, and keep batch sizes moderate. - Use
SYS_REFCURSORwhen the query must be chosen at run time or returned to a caller.
> Note: Use the FOR UPDATE clause only when you need to change the fetched rows, because the lock blocks other sessions until you commit.
Exercise
A procedure must update salaries for employees in a department passed as a parameter. Which cursor feature lets you lock the rows and update the current row inside a loop?
Show answer
Declare the cursor with FOR UPDATE (optionally NOWAIT), then use UPDATE ... WHERE CURRENT OF cursor_name inside the loop.