Sub topic 13
Common Cursor Errors and Closing Cursors
This chapter assumes you know basic SQL and can write an anonymous PL/SQL block with variables, loops, and exception handlers. It focuses on explicit cursor cleanup and the errors that occur when a cursor is used in the wrong state.
Why cursor state matters
An explicit cursor has a lifecycle:
- Declare the cursor.
- Open the cursor.
- Fetch rows from it.
- Close the cursor.
Oracle tracks this state in the cursor's context area. A cursor is open only between OPEN and CLOSE.
If a block opens a cursor but leaves it open, the session keeps resources associated with that cursor until Oracle cleans them up. The safest practice is to close an explicitly opened cursor in both the normal path and the exception path.
Common invalid operations
These operations commonly raise cursor errors:
| Operation | Problem |
|---|---|
| `OPEN c` when `c` is already open | The cursor is already active |
| `FETCH c` before `OPEN c` | The cursor has not been opened |
| `FETCH c` after `CLOSE c` | The cursor is no longer active |
| `CLOSE c` before `OPEN c` | There is no active cursor to close |
| `CLOSE c` twice | The second close uses an invalid cursor |
The exact Oracle error is usually INVALID_CURSOR for an operation performed in the wrong state. A FETCH can also raise a conversion or value error if the target variables do not match the selected columns.
Closing a cursor safely
Use %ISOPEN before closing a manually managed cursor. This makes the exception handler safe whether the error happened before or after OPEN.
DECLARE
CURSOR c_emp IS
SELECT employee_id, last_name, salary
FROM employees
WHERE department_id = 50;
l_emp_id employees.employee_id%TYPE;
l_name employees.last_name%TYPE;
l_salary employees.salary%TYPE;
BEGIN
OPEN c_emp;
LOOP
FETCH c_emp INTO l_emp_id, l_name, l_salary;
EXIT WHEN c_emp%NOTFOUND;
DBMS_OUTPUT.PUT_LINE(l_name || ': ' || l_salary);
END LOOP;
CLOSE c_emp;
EXCEPTION
WHEN OTHERS THEN
IF c_emp%ISOPEN THEN
CLOSE c_emp;
END IF;
RAISE;
END;
/The RAISE statement sends the original error to the caller after cleanup. Without it, the handler would hide the failure and make the block appear successful.
A common mistake is to close the cursor only after the loop. If an exception occurs during a fetch or while processing a row, execution jumps directly to the exception handler and skips that normal CLOSE.
Prefer cursor FOR loops when possible
A cursor FOR loop manages OPEN, FETCH, and CLOSE automatically. It also declares the loop record for you. This removes several opportunities for state errors.
BEGIN
FOR r_emp IN (
SELECT employee_id, last_name, salary
FROM employees
WHERE department_id = 50
ORDER BY last_name
)
LOOP
DBMS_OUTPUT.PUT_LINE(r_emp.last_name || ': ' || r_emp.salary);
IF r_emp.salary < 3000 THEN
RAISE_APPLICATION_ERROR(-20001, 'Salary is below the required minimum');
END IF;
END LOOP;
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Processing failed: ' || SQLERRM);
RAISE;
END;
/Even when the loop ends because of an exception, Oracle manages the cursor opened for that loop. Use a manually controlled cursor only when you need explicit lifecycle control, such as passing it between operations, fetching in a special pattern, or using WHERE CURRENT OF.
A practical choice
Use a single SQL statement when the database can perform the entire operation. Use a cursor FOR loop when you must process each row with procedural logic. Use an explicit cursor when you need manual control over opening, fetching, or closing. For large result sets, consider BULK COLLECT ... LIMIT to reduce the number of fetch operations while controlling memory use.
Exercise
What should an exception handler do before re-raising an error from a block that manually opened cursor c_emp?
Show answer
Test c_emp%ISOPEN, close the cursor only when it is open, and then execute RAISE to preserve the original error.