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-01002 means a FETCH came at a point where the cursor cannot return rows any more. In PL/SQL the most common case is a SELECT ... FOR UPDATE cursor: COMMIT or ROLLBACK releases its row locks, and the next fetch from that cursor fails.
The error is raised at the fetch, so ORA-06512 points to the line of the FOR loop or FETCH, not to the COMMIT that caused it. In all three tests below, the first row was processed and the error came at the next fetch.
01COMMIT inside a FOR UPDATE cursor loop
The loop locks the JOBS rows with FOR UPDATE and commits after each row. Job 1 printed done 1, then the next fetch failed with ORA-01002 at line 2 (the FOR line). The commit for job 1 had already gone through.
The statuses were set back to NEW, and the COMMIT was moved after END LOOP. Then all three jobs printed done and all three rows became DONE.
Setup
CREATE TABLE jobs (job_id NUMBER PRIMARY KEY, status VARCHAR2(10));
INSERT INTO jobs VALUES (1, 'NEW');
INSERT INTO jobs VALUES (2, 'NEW');
INSERT INTO jobs VALUES (3, 'NEW');
COMMIT;Table created.
1 row created.
1 row created.
1 row created.
Commit complete.SET SERVEROUTPUT ON
BEGIN
FOR r IN (SELECT job_id FROM jobs FOR UPDATE) LOOP
UPDATE jobs SET status = 'DONE' WHERE job_id = r.job_id;
COMMIT;
DBMS_OUTPUT.PUT_LINE('done ' || r.job_id);
END LOOP;
END;
/done 1
BEGIN
*
ERROR at line 1:
ORA-01002: fetch out of sequence
ORA-06512: at line 2
ORA-06512: at line 2
Help: https://docs.oracle.com/error-help/db/ora-01002/UPDATE jobs SET status = 'NEW';
COMMIT;
SELECT job_id, status FROM jobs ORDER BY job_id;3 rows updated.
Commit complete.
JOB_ID STATUS
---------- ------------------------------
1 NEW
2 NEW
3 NEW
3 rows selected.Commit once after the loop. If you really need to commit in batches, do not use FOR UPDATE for that cursor.
SET SERVEROUTPUT ON
BEGIN
FOR r IN (SELECT job_id FROM jobs FOR UPDATE) LOOP
UPDATE jobs SET status = 'DONE' WHERE job_id = r.job_id;
DBMS_OUTPUT.PUT_LINE('done ' || r.job_id);
END LOOP;
COMMIT;
END;
/
SELECT job_id, status FROM jobs ORDER BY job_id;done 1
done 2
done 3
PL/SQL procedure successfully completed.
JOB_ID STATUS
---------- ------------------------------
1 DONE
2 DONE
3 DONE
3 rows selected.02ROLLBACK in an exception handler inside the loop
Each order is updated with WHERE CURRENT OF c, and order 2 has qty = 0, so ZERO_DIVIDE is raised. The handler ran ROLLBACK and printed skip 2, but that ROLLBACK also released the cursor's locks. The next fetch failed with ORA-01002, and no order was changed.
With SAVEPOINT before_row at the start of each row and ROLLBACK TO before_row in the handler, only order 2 was undone: orders 1 and 3 became DONE, and order 2 stayed NEW.
Setup
CREATE TABLE orders (order_id NUMBER PRIMARY KEY, qty NUMBER, unit_price NUMBER, status VARCHAR2(10));
INSERT INTO orders VALUES (1, 2, 100, 'NEW');
INSERT INTO orders VALUES (2, 0, 100, 'NEW');
INSERT INTO orders VALUES (3, 5, 100, 'NEW');
COMMIT;Table created.
1 row created.
1 row created.
1 row created.
Commit complete.SET SERVEROUTPUT ON
DECLARE
CURSOR c IS SELECT order_id, qty FROM orders WHERE status = 'NEW' FOR UPDATE;
v_avg NUMBER;
BEGIN
FOR r IN c LOOP
BEGIN
UPDATE orders SET status = 'DONE' WHERE CURRENT OF c;
v_avg := 1000 / r.qty;
DBMS_OUTPUT.PUT_LINE('ok ' || r.order_id);
EXCEPTION
WHEN ZERO_DIVIDE THEN
ROLLBACK;
DBMS_OUTPUT.PUT_LINE('skip ' || r.order_id);
END;
END LOOP;
COMMIT;
END;
/ok 1
skip 2
DECLARE
*
ERROR at line 1:
ORA-01002: fetch out of sequence
ORA-06512: at line 5
ORA-06512: at line 5
Help: https://docs.oracle.com/error-help/db/ora-01002/SELECT order_id, qty, status FROM orders ORDER BY order_id; ORDER_ID QTY STATUS
---------- ---------- ------------------------------
1 2 NEW
2 0 NEW
3 5 NEW
3 rows selected.ROLLBACK TO SAVEPOINT undoes only the work after the savepoint and keeps the FOR UPDATE cursor usable. A full ROLLBACK does not.
SET SERVEROUTPUT ON
DECLARE
CURSOR c IS SELECT order_id, qty FROM orders WHERE status = 'NEW' FOR UPDATE;
v_avg NUMBER;
BEGIN
FOR r IN c LOOP
SAVEPOINT before_row;
BEGIN
UPDATE orders SET status = 'DONE' WHERE CURRENT OF c;
v_avg := 1000 / r.qty;
DBMS_OUTPUT.PUT_LINE('ok ' || r.order_id);
EXCEPTION
WHEN ZERO_DIVIDE THEN
ROLLBACK TO before_row;
DBMS_OUTPUT.PUT_LINE('skip ' || r.order_id);
END;
END LOOP;
COMMIT;
END;
/
SELECT order_id, qty, status FROM orders ORDER BY order_id;ok 1
skip 2
ok 3
PL/SQL procedure successfully completed.
ORDER_ID QTY STATUS
---------- ---------- ------------------------------
1 2 DONE
2 0 NEW
3 5 DONE
3 rows selected.03DBMS_SQL.FETCH_ROWS called again after it returned 0
The code first calls DBMS_SQL.FETCH_ROWS to check whether there are rows, then calls it again in a WHILE loop. Department 20 has no employees (COUNT(*) was 0), so the first call returned 0 and printed no rows. The second call came after the end of the result and failed with ORA-01002 at SYS.DBMS_SQL.
Calling FETCH_ROWS only in the loop and counting the rows fixed it: it printed no rows and finished normally.
Setup
CREATE TABLE emp (emp_id NUMBER, name VARCHAR2(20), dept_id NUMBER);
INSERT INTO emp VALUES (1, 'KIM', 10);
INSERT INTO emp VALUES (2, 'LEE', 10);
COMMIT;Table created.
1 row created.
1 row created.
Commit complete.SET SERVEROUTPUT ON
DECLARE
c INTEGER := DBMS_SQL.OPEN_CURSOR;
n INTEGER;
v_name VARCHAR2(20);
BEGIN
DBMS_SQL.PARSE(c, 'SELECT name FROM emp WHERE dept_id = :d', DBMS_SQL.NATIVE);
DBMS_SQL.BIND_VARIABLE(c, ':d', 20);
DBMS_SQL.DEFINE_COLUMN(c, 1, v_name, 20);
n := DBMS_SQL.EXECUTE(c);
IF DBMS_SQL.FETCH_ROWS(c) = 0 THEN
DBMS_OUTPUT.PUT_LINE('no rows');
END IF;
WHILE DBMS_SQL.FETCH_ROWS(c) > 0 LOOP
DBMS_SQL.COLUMN_VALUE(c, 1, v_name);
DBMS_OUTPUT.PUT_LINE(v_name);
END LOOP;
DBMS_SQL.CLOSE_CURSOR(c);
END;
/no rows
DECLARE
*
ERROR at line 1:
ORA-01002: fetch out of sequence
ORA-06512: at "SYS.DBMS_SQL", line 1843
ORA-06512: at line 13
Help: https://docs.oracle.com/error-help/db/ora-01002/SELECT COUNT(*) FROM emp WHERE dept_id = 20; COUNT(*)
----------
0
1 row selected.Once FETCH_ROWS returns 0, do not call it again for that execution. The error only shows up when the result is empty, so it is easy to miss in testing.
SET SERVEROUTPUT ON
DECLARE
c INTEGER := DBMS_SQL.OPEN_CURSOR;
n INTEGER;
v_cnt INTEGER := 0;
v_name VARCHAR2(20);
BEGIN
DBMS_SQL.PARSE(c, 'SELECT name FROM emp WHERE dept_id = :d', DBMS_SQL.NATIVE);
DBMS_SQL.BIND_VARIABLE(c, ':d', 20);
DBMS_SQL.DEFINE_COLUMN(c, 1, v_name, 20);
n := DBMS_SQL.EXECUTE(c);
WHILE DBMS_SQL.FETCH_ROWS(c) > 0 LOOP
DBMS_SQL.COLUMN_VALUE(c, 1, v_name);
DBMS_OUTPUT.PUT_LINE(v_name);
v_cnt := v_cnt + 1;
END LOOP;
IF v_cnt = 0 THEN
DBMS_OUTPUT.PUT_LINE('no rows');
END IF;
DBMS_SQL.CLOSE_CURSOR(c);
END;
/no rows
PL/SQL procedure successfully completed.FAQ
Can I COMMIT inside a cursor loop in Oracle?
Not when the cursor is SELECT ... FOR UPDATE. In the test, the first COMMIT inside the loop made the next fetch fail with ORA-01002. Move the COMMIT after the loop.
Why does ORA-01002 point to the FOR loop line, not the COMMIT?
The COMMIT itself succeeds. The error is raised at the next fetch, which in a cursor FOR loop happens on the FOR line.
How do I skip one failed row without ORA-01002?
Set SAVEPOINT at the start of each row and use ROLLBACK TO that savepoint in the exception handler. In the test, only the failed row was undone and the loop finished.
Is a row committed before ORA-01002 rolled back?
No. In the test, the update for job 1 was already committed before the error. Check the data before running the job again.
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