# SQLCODE -507: Cursor not open for UPDATE or DELETE

Source: https://wndsoftware.com/mainframe-errors/sqlcode-507

### SQLCODE -507: Cursor not open for UPDATE or DELETE

SQLCODE -507 (SQLSTATE 24501) means the program ran an UPDATE or DELETE ... WHERE CURRENT OF on a cursor that isn't open. The usual reason is a COMMIT or ROLLBACK that closed the cursor between the FETCH and the update.


#### Other names for this code

Also written as `SQL -507`, `SQLCODE=-507`, `SQLCODE = -507`, `db2 -507`, `sqlstate 24501`, `SQL0507N`, `db2 cursor not open update delete`, `SQL0507`.


### What it means

A positioned UPDATE or DELETE acts on the row the cursor is on, so the cursor has to be open. If it isn't, there's no current row, and Db2 rejects the statement with -507 and changes nothing.

Cursors close in more ways than an explicit CLOSE. A COMMIT closes every cursor not declared WITH HOLD. A ROLLBACK closes every cursor, WITH HOLD or not, and that includes the rollback Db2 does for a deadlock or timeout reported as [-911](https://wndsoftware.com/mainframe-errors/sqlcode-911). In CICS, a SYNCPOINT is a commit.

The same situation on a FETCH or CLOSE is [-501](https://wndsoftware.com/mainframe-errors/sqlcode-501). -507 is the code for positioned UPDATE and DELETE.


### Common causes

- A batch program commits every N rows inside its fetch-and-update loop, and the cursor isn't declared WITH HOLD, so the first update after the commit fails.
- A -911 deadlock or timeout rolled back the unit of work and closed the cursor, and the program carried on with the next update.
- An earlier SQL error closed the cursor. IBM lists codes such as -404, -802, -904 and -913 among those that can force a cursor closed, so check the statements before the failing one.
- The OPEN failed with a negative SQLCODE that wasn't checked, so the cursor was never open.
- The program closed the cursor on one path and then reached the update on another.
- In CICS, a SYNCPOINT, or a called program that issued one, ended the unit of work in the middle of the loop.


### How to find the problem

- Look at what happened between the last successful FETCH and the failing UPDATE or DELETE. Look for COMMIT, ROLLBACK, SYNCPOINT, CLOSE and calls to other programs.
- Check the SQLCODE of the OPEN and of every statement just before the failure. A -911, -913, -904 or -802 earlier in the job log is a strong clue.
- Check the DECLARE CURSOR for WITH HOLD.


### How to fix it

- If the program commits inside the loop, declare the cursor WITH HOLD so it stays open and positioned after each COMMIT.
- After a ROLLBACK or -911, reopen the cursor and reposition it from the last committed key before going on, or end the step for a restart.
- Check the SQLCODE after every OPEN and FETCH before issuing a positioned UPDATE or DELETE.
- Move commits to a point in the loop after the positioned statements for the current row have run.


### IBM documentation

[IBM documentation: -507](https://www.ibm.com/docs/en/db2-for-zos/13.0.0?topic=codes-507). Use the documentation for your installed release.

[Report a correction to WND Software](https://wndsoftware.com/contact#message).

Technical reviewer: [Albert Joseph](https://wndsoftware.com/authors/albert-joseph), WND Software.

Last reviewed 2026-10-18 by the WND Software team. Written in our own words. For the full reference, check IBM’s Db2 for z/OS documentation for your release.


### Related codes

[SQLCODE -501 Cursor not open Read →](https://wndsoftware.com/mainframe-errors/sqlcode-501)
[SQLCODE -508 Cursor not positioned on a row Read →](https://wndsoftware.com/mainframe-errors/sqlcode-508)
[SQLCODE -502 Cursor already open Read →](https://wndsoftware.com/mainframe-errors/sqlcode-502)
[SQLCODE -911 Deadlock or timeout, rolled back Read →](https://wndsoftware.com/mainframe-errors/sqlcode-911)
[SQLCODE -504 Cursor name not declared Read →](https://wndsoftware.com/mainframe-errors/sqlcode-504)

### Questions


#### My cursor is WITH HOLD. Why did it close?

WITH HOLD keeps a cursor open across a COMMIT, not a ROLLBACK. A deadlock or timeout reported as -911 rolls back the unit of work and closes every cursor.


#### What is the difference between -507 and -501?

Both mean the cursor isn't open. -501 is returned for FETCH or CLOSE, and -507 for UPDATE or DELETE WHERE CURRENT OF.


#### Is it the same on Db2 for LUW?

Yes. Db2 for LUW reports SQL0507N with SQLSTATE 24501.

[Db2 SQLCODEs](https://wndsoftware.com/mainframe-errors/db2-sqlcodes) · [SQLCODE -504](https://wndsoftware.com/mainframe-errors/sqlcode-504) · [SQLCODE -508](https://wndsoftware.com/mainframe-errors/sqlcode-508)

[← All error codes](https://wndsoftware.com/mainframe-errors/)
