Sub topic 11
REF CURSOR and SYS_REFCURSOR
This chapter assumes that you know basic SELECT statements, PL/SQL blocks, variables, loops, and exception handling. It also assumes that you understand the lifecycle of an explicit cursor: declare, open, fetch, and close.
What a REF CURSOR is
A REF CURSOR is a cursor variable. Unlike a normal explicit cursor, it does not identify one fixed query at declaration time. A program can open the variable for a query while it runs, then pass the open cursor to another program unit.
SYS_REFCURSOR is Oracle's predefined weakly typed REF CURSOR type. “Weakly typed” means that the variable can point to queries with different select lists. The code that fetches from it must use variables whose types and order match the query's result.
A reference cursor has a state. Before OPEN FOR, it is closed. After OPEN FOR, it points to the query result and can be fetched. After CLOSE, it cannot be fetched again until it is opened again.
Opening and passing SYS_REFCURSOR
The following local procedure opens a query and returns the cursor through an OUT parameter. The calling block receives the cursor, fetches each row, and closes it.
DECLARE
l_rc SYS_REFCURSOR;
l_last_name employees.last_name%TYPE;
l_salary employees.salary%TYPE;
PROCEDURE find_employees(
p_department_id IN employees.department_id%TYPE,
p_result OUT SYS_REFCURSOR
) IS
BEGIN
OPEN p_result FOR
SELECT last_name, salary
FROM employees
WHERE department_id = p_department_id
ORDER BY last_name;
END find_employees;
BEGIN
find_employees(50, l_rc);
LOOP
FETCH l_rc INTO l_last_name, l_salary;
EXIT WHEN l_rc%NOTFOUND;
DBMS_OUTPUT.PUT_LINE(
l_last_name || ': ' || l_salary
);
END LOOP;
CLOSE l_rc;
END;
/The query is parsed and opened by find_employees, but the rows are fetched by the caller. This separation is useful when a procedure supplies data to another procedure, an application, or a reporting layer.
A SYS_REFCURSOR does not copy every result row into the PL/SQL variable. It represents the open result set, so the cursor remains tied to database resources until it is closed.
> callout note > Use bind variables or PL/SQL parameters in OPEN FOR queries. Do not build SQL by concatenating untrusted input into the statement text.
Strong and weak reference cursors
You can define a strong reference cursor when every query must return a known row shape.
DECLARE
TYPE t_emp_cursor IS REF CURSOR
RETURN employees%ROWTYPE;
l_rc t_emp_cursor;
l_emp employees%ROWTYPE;
BEGIN
OPEN l_rc FOR
SELECT *
FROM employees
WHERE department_id = 50;
FETCH l_rc INTO l_emp;
CLOSE l_rc;
END;
/The strong type provides more compile-time checking, but it is less flexible. SYS_REFCURSOR is easier to pass through general-purpose interfaces, especially when different callers need different queries.
Common errors and choices
- Fetch columns in the same order as the
SELECTlist. - Use compatible data types for every fetched column.
- Do not fetch from a cursor that has not been opened.
- Close an open cursor on every normal completion path.
- Close it in exception handling when a block can fail after opening it.
- Prefer one SQL statement when SQL can perform the complete operation.
- Use a cursor FOR loop for simple row-by-row PL/SQL processing.
- Use
SYS_REFCURSORwhen a query result must cross a procedure or API boundary. - Use bulk processing when many rows must be handled in PL/SQL.
Exercise
Why might a procedure return a SYS_REFCURSOR instead of returning a collection of rows?
Show answer
A SYS_REFCURSOR lets the caller fetch the result incrementally and avoids requiring the procedure to materialize the entire result in a PL/SQL collection.