Sub topic 3
Explicit Cursors: Declare, Open, Fetch, Close
This chapter assumes that you know basic SELECT statements and PL/SQL blocks. You should also know variables, loops, and basic exception handling.
An explicit cursor is a named object that represents the result of a query. It lets a PL/SQL program process the returned rows one at a time. You control its lifecycle:
- Declare the cursor and its query.
- Open the cursor.
- Fetch rows from it.
- Close the cursor.
A cursor does not copy every result into a PL/SQL variable at once. Oracle keeps the query's result in a context area and tracks the current position as rows are fetched.
Declaring and opening a cursor
Declare an explicit cursor in the DECLARE section of a PL/SQL block. The declaration contains a query, but Oracle does not run that query until the cursor is opened.
DECLARE
CURSOR c_emp IS
SELECT employee_id, last_name, salary
FROM employees
WHERE department_id = 50
ORDER BY last_name;
BEGIN
OPEN c_emp;
-- Fetch rows here.
CLOSE c_emp;
END;
/OPEN runs the query and positions the cursor before the first row. A cursor must be open before you fetch from it.
Fetching rows into variables
The FETCH statement moves the cursor to the next row and copies column values into variables. The target variables must match the selected columns in number and compatible type.
DECLARE
CURSOR c_emp IS
SELECT employee_id, last_name, salary
FROM employees
WHERE department_id = 50
ORDER BY last_name;
l_employee_id employees.employee_id%TYPE;
l_last_name employees.last_name%TYPE;
l_salary employees.salary%TYPE;
BEGIN
OPEN c_emp;
LOOP
FETCH c_emp INTO l_employee_id, l_last_name, l_salary;
EXIT WHEN c_emp%NOTFOUND;
DBMS_OUTPUT.PUT_LINE(
l_employee_id || ' ' || l_last_name || ': ' || l_salary
);
END LOOP;
CLOSE c_emp;
END;
/The EXIT WHEN test must come after FETCH. When the fetch moves past the final row, %NOTFOUND becomes true. The target variables can still contain values from the previous successful fetch, so process the row only after checking %NOTFOUND.
Fetching into a record
A record groups related values into fields. A cursor-based %ROWTYPE record gets fields that match the cursor's select list.
DECLARE
CURSOR c_emp IS
SELECT employee_id, last_name, salary
FROM employees
WHERE department_id = 50;
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.last_name || ': ' || r_emp.salary);
END LOOP;
CLOSE c_emp;
END;
/Using %ROWTYPE avoids declaring one variable for every selected column. If you change the cursor's select list, Oracle also updates the record's structure.
A safe cleanup pattern
DECLARE
CURSOR c_emp IS
SELECT last_name
FROM employees
WHERE department_id = 50;
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.last_name);
END LOOP;
CLOSE c_emp;
EXCEPTION
WHEN OTHERS THEN
IF c_emp%ISOPEN THEN
CLOSE c_emp;
END IF;
RAISE;
END;
/The exception handler closes the cursor before re-raising the original error.
Exercise
In an explicit cursor loop, where should EXIT WHEN c_emp%NOTFOUND appear, and why?
Show answer
It should appear immediately after FETCH. The fetch must first attempt to move to the next row, and %NOTFOUND then tells the loop whether a row was returned.