Db2 SQLCODE

SQLCODE -502: Cursor already open

SQLCODE -502 (SQLSTATE 24502) means the program ran an OPEN for a cursor that was already open. Db2 rejects the second OPEN, and the cursor stays open where it was.

Other names for this code

Also written as SQL -502, SQLCODE=-502, SQLCODE = -502, db2 -502, sqlstate 24502, SQL0502N, db2 cursor already open, SQL0502.

What it means

A cursor has two states, open and closed. OPEN runs the query and positions the cursor before the first row. CLOSE ends it. Opening a cursor that is already open isn't allowed, because Db2 would have to throw away the current result and position without being asked. So it returns -502 and leaves the open cursor alone.

A cursor stays open until the program closes it, the unit of work ends, or an error makes Db2 close it. A COMMIT closes it unless it was declared WITH HOLD. A ROLLBACK, including the one behind a -911, closes every cursor.

A cursor isn't closed when the program that opened it returns to its caller. So a subprogram called many times in the same unit of work finds its cursor still open from the last call unless it closed it.

Common causes

  • A loop opens the cursor for each key, and one exit path, such as an error branch or an early end, skips the CLOSE.
  • The cursor is declared WITH HOLD, so it survived a COMMIT that the program expected to close it.
  • A subprogram is called again in the same unit of work and opens a cursor it left open on the previous call.
  • Error handling retries the work and goes back to the OPEN without closing the cursor first.
  • Two paragraphs or sections both open the same cursor, and a change in the program's flow made both run.

How to find the problem

  1. Find every OPEN and CLOSE of that cursor in the program, and follow each path between them, especially error and end-of-data paths.
  2. Check the DECLARE CURSOR for WITH HOLD.
  3. Add a display or trace of each OPEN and CLOSE with the key being processed, and rerun to see which path runs twice.
  4. If the program is called as a subprogram, check whether the caller expects the cursor to be closed when control returns.

How to fix it

  • Close the cursor on every exit path, including error branches.
  • When restarting work, issue CLOSE before OPEN and treat -501 (cursor not open) from that CLOSE as normal.
  • Keep a switch that records whether the cursor is open, and test it before OPEN and CLOSE.
  • Use WITH HOLD only where you want the cursor to survive a COMMIT, and design the restart logic around it.

IBM documentation

IBM documentation: -502. Use the documentation for your installed release.

Report a correction to WND Software.

Questions

Can I ignore -502 and carry on fetching?

Only if you meant to continue from where the cursor already was. The second OPEN did nothing, so the cursor still has its old result and position.

Doesn't COMMIT close all cursors?

Not cursors declared WITH HOLD. They stay open and positioned after a COMMIT. A ROLLBACK closes all cursors.

Is it the same on Db2 for LUW and IBM i?

Yes. Db2 for LUW reports SQL0502N and IBM i reports SQL0502, both with SQLSTATE 24502.