Oracle 23.26PL/SQLLab-verified · 2026-11-24

ORA-06511: PL/SQL: cursor already open

An explicit cursor was opened again while it was still open.

Oracle AI Database 26ai Free 23.26.3.0.0
ORA-06511: PL/SQL: cursor already open

Jump to a cause

  1. 01OPEN inside a loop without CLOSE
  2. 02Opening the cursor before a cursor FOR loop
  3. 03A package cursor left open between calls

Lab-verifiedReproduced and fixed on Oracle AI Database 26ai Free 23.26.3.0.0 · 2026-11-24 · container image gvenzl/oracle-free:23-slim

ORA-06511 is raised when OPEN runs on an explicit cursor that is already open. A cursor can be open only once at a time; it has to be closed before it can be opened again.

The error points to the line of the OPEN (or the cursor FOR loop), but the real mistake is usually somewhere else: a missing CLOSE, or an extra OPEN. In a package, the cursor even stays open after the procedure ends, so the first call works and the second call fails.

01OPEN inside a loop without CLOSE

The block opens c_orders(i) in each loop iteration but never closes it. The first iteration printed cust 1: 800; in the second iteration OPEN failed with ORA-06511 because the cursor from the first iteration was still open. Passing a different parameter does not make it a new cursor.

Checking c_orders%ISOPEN shows the state: YES after FETCH, NO only after CLOSE. Fetching a row does not close the cursor.

Adding CLOSE c_orders; after the FETCH fixed it: both cust 1: 800 and cust 2: 700 were printed.

Setup
Setuprun as lab_a
CREATE TABLE orders (order_id NUMBER PRIMARY KEY, cust_id NUMBER, amount NUMBER);
INSERT INTO orders VALUES (1001, 1, 500);
INSERT INTO orders VALUES (1002, 1, 300);
INSERT INTO orders VALUES (1003, 2, 700);
COMMIT;
OUTPUT
Table created.


1 row created.


1 row created.


1 row created.


Commit complete.
Reproducerun as lab_a
SET SERVEROUTPUT ON
DECLARE
  CURSOR c_orders(p_cust NUMBER) IS SELECT SUM(amount) FROM orders WHERE cust_id = p_cust;
  v_sum NUMBER;
BEGIN
  FOR i IN 1 .. 2 LOOP
    OPEN c_orders(i);
    FETCH c_orders INTO v_sum;
    DBMS_OUTPUT.PUT_LINE('cust ' || i || ': ' || v_sum);
  END LOOP;
END;
/
✕ ERROR
cust 1: 800
DECLARE
*
ERROR at line 1:
ORA-06511: PL/SQL: cursor already open
ORA-06512: at line 2
ORA-06512: at line 6
Help: https://docs.oracle.com/error-help/db/ora-06511/
Checkrun as lab_a
SET SERVEROUTPUT ON
DECLARE
  CURSOR c_orders(p_cust NUMBER) IS SELECT SUM(amount) FROM orders WHERE cust_id = p_cust;
  v_sum NUMBER;
BEGIN
  OPEN c_orders(1);
  FETCH c_orders INTO v_sum;
  DBMS_OUTPUT.PUT_LINE('after FETCH, open: ' || CASE WHEN c_orders%ISOPEN THEN 'YES' ELSE 'NO' END);
  CLOSE c_orders;
  DBMS_OUTPUT.PUT_LINE('after CLOSE, open: ' || CASE WHEN c_orders%ISOPEN THEN 'YES' ELSE 'NO' END);
END;
/
OUTPUT
after FETCH, open: YES
after CLOSE, open: NO

PL/SQL procedure successfully completed.

Every OPEN needs a CLOSE on the same path, including inside loops.

After the fixrun as lab_a
SET SERVEROUTPUT ON
DECLARE
  CURSOR c_orders(p_cust NUMBER) IS SELECT SUM(amount) FROM orders WHERE cust_id = p_cust;
  v_sum NUMBER;
BEGIN
  FOR i IN 1 .. 2 LOOP
    OPEN c_orders(i);
    FETCH c_orders INTO v_sum;
    CLOSE c_orders;
    DBMS_OUTPUT.PUT_LINE('cust ' || i || ': ' || v_sum);
  END LOOP;
END;
/
✓ SUCCESS
cust 1: 800
cust 2: 700

PL/SQL procedure successfully completed.

02Opening the cursor before a cursor FOR loop

FOR r IN c_orders LOOP opens, fetches and closes the cursor by itself. The block called OPEN c_orders; first, so the FOR loop tried to open an already open cursor and failed before printing any row.

Printing c_orders%ISOPEN around a plain FOR loop shows this: NO before the loop, YES inside it, NO again after it.

Removing the OPEN line was enough: the loop printed the three orders 1001, 1002, 1003.

Setup
Setuprun as lab_a
CREATE TABLE orders (order_id NUMBER PRIMARY KEY, cust_id NUMBER, amount NUMBER);
INSERT INTO orders VALUES (1001, 1, 500);
INSERT INTO orders VALUES (1002, 1, 300);
INSERT INTO orders VALUES (1003, 2, 700);
COMMIT;
OUTPUT
Table created.


1 row created.


1 row created.


1 row created.


Commit complete.
Reproducerun as lab_a
SET SERVEROUTPUT ON
DECLARE
  CURSOR c_orders IS SELECT order_id, amount FROM orders ORDER BY order_id;
BEGIN
  OPEN c_orders;
  FOR r IN c_orders LOOP
    DBMS_OUTPUT.PUT_LINE(r.order_id || ' ' || r.amount);
  END LOOP;
END;
/
✕ ERROR
DECLARE
*
ERROR at line 1:
ORA-06511: PL/SQL: cursor already open
ORA-06512: at line 2
ORA-06512: at line 5
Help: https://docs.oracle.com/error-help/db/ora-06511/
Checkrun as lab_a
SET SERVEROUTPUT ON
DECLARE
  CURSOR c_orders IS SELECT order_id, amount FROM orders ORDER BY order_id;
BEGIN
  DBMS_OUTPUT.PUT_LINE('before loop, open: ' || CASE WHEN c_orders%ISOPEN THEN 'YES' ELSE 'NO' END);
  FOR r IN c_orders LOOP
    DBMS_OUTPUT.PUT_LINE('in loop ' || r.order_id || ', open: ' || CASE WHEN c_orders%ISOPEN THEN 'YES' ELSE 'NO' END);
  END LOOP;
  DBMS_OUTPUT.PUT_LINE('after loop, open: ' || CASE WHEN c_orders%ISOPEN THEN 'YES' ELSE 'NO' END);
END;
/
OUTPUT
before loop, open: NO
in loop 1001, open: YES
in loop 1002, open: YES
in loop 1003, open: YES
after loop, open: NO

PL/SQL procedure successfully completed.

Use either OPEN/FETCH/CLOSE or a cursor FOR loop, not both on the same cursor.

After the fixrun as lab_a
SET SERVEROUTPUT ON
DECLARE
  CURSOR c_orders IS SELECT order_id, amount FROM orders ORDER BY order_id;
BEGIN
  FOR r IN c_orders LOOP
    DBMS_OUTPUT.PUT_LINE(r.order_id || ' ' || r.amount);
  END LOOP;
END;
/
✓ SUCCESS
1001 500
1002 300
1003 700

PL/SQL procedure successfully completed.

03A package cursor left open between calls

c_orders is declared in the package body, so it lives for the whole session, not just for one call. first_order opened it and fetched one row without closing it. The first EXEC printed first: 1001; the second EXEC in the same session failed with ORA-06511 at LAB_A.ORDER_PKG.

The fixed procedure closes the cursor if c_orders%ISOPEN is true, opens it, fetches, and closes it again. After that, two calls in a row both printed first: 1001.

Setup
Setuprun as lab_a
CREATE TABLE orders (order_id NUMBER PRIMARY KEY, cust_id NUMBER, amount NUMBER);
INSERT INTO orders VALUES (1001, 1, 500);
INSERT INTO orders VALUES (1002, 1, 300);
INSERT INTO orders VALUES (1003, 2, 700);
COMMIT;
CREATE OR REPLACE PACKAGE order_pkg AS
  PROCEDURE first_order;
END;
/
CREATE OR REPLACE PACKAGE BODY order_pkg AS
  CURSOR c_orders IS SELECT order_id FROM orders ORDER BY order_id;
  PROCEDURE first_order IS
    v_id NUMBER;
  BEGIN
    OPEN c_orders;
    FETCH c_orders INTO v_id;
    DBMS_OUTPUT.PUT_LINE('first: ' || v_id);
  END;
END;
/
OUTPUT
Table created.


1 row created.


1 row created.


1 row created.


Commit complete.


Package created.


Package body created.
Reproducerun as lab_a
SET SERVEROUTPUT ON
EXEC order_pkg.first_order
EXEC order_pkg.first_order
✕ ERROR
first: 1001

PL/SQL procedure successfully completed.

BEGIN order_pkg.first_order; END;

*
ERROR at line 1:
ORA-06511: PL/SQL: cursor already open
ORA-06512: at "LAB_A.ORDER_PKG", line 2
ORA-06512: at "LAB_A.ORDER_PKG", line 6
ORA-06512: at line 1
Help: https://docs.oracle.com/error-help/db/ora-06511/
Fixrun as lab_a
CREATE OR REPLACE PACKAGE BODY order_pkg AS
  CURSOR c_orders IS SELECT order_id FROM orders ORDER BY order_id;
  PROCEDURE first_order IS
    v_id NUMBER;
  BEGIN
    IF c_orders%ISOPEN THEN
      CLOSE c_orders;
    END IF;
    OPEN c_orders;
    FETCH c_orders INTO v_id;
    CLOSE c_orders;
    DBMS_OUTPUT.PUT_LINE('first: ' || v_id);
  END;
END;
/
OUTPUT
Package body created.

Package-level cursors keep their state per session. Close them in the procedure, and check %ISOPEN before OPEN if an earlier call may have failed halfway.

After the fixrun as lab_a
SET SERVEROUTPUT ON
EXEC order_pkg.first_order
EXEC order_pkg.first_order
✓ SUCCESS
first: 1001

PL/SQL procedure successfully completed.

first: 1001

PL/SQL procedure successfully completed.

FAQ

Why does ORA-06511 happen only on the second call of my procedure?

The cursor is probably declared at package level. It stays open after the first call ends, and the second call's OPEN fails. In the test, the first EXEC worked and the second one raised ORA-06511.

How do I check if a cursor is already open?

Use cursor_name%ISOPEN. The fix in the test was IF c_orders%ISOPEN THEN CLOSE c_orders; END IF; before OPEN.

Do I need OPEN before a cursor FOR loop?

No. The FOR loop opens and closes the cursor itself. An OPEN before it causes ORA-06511.

Does opening a cursor with a different parameter avoid ORA-06511?

No. In the test, OPEN c_orders(2) failed while c_orders(1) was still open. Close the cursor first.

How we verify

Each scenario starts from fresh database users. A script runs the setup, confirms the exact error code, applies the fix and runs the statement again. The output shown is what the database printed, saved by the script.

lab_a and lab_b are ordinary users created for the test; system is the DBA account. Oracle AI Database 26ai Free Release 23.26.3.0.0 - Develop, Learn, and Run for Free 23.26.3.0.0