MainframeKit

-502

SQLSTATE 24502NEGATIVE SQLCODE

The program issued OPEN for a cursor that is already open. The OPEN is rejected and the cursor stays as it was.

MOST LIKELY

A loop that opens the cursor on each pass but closes it only on some paths.

FIRST CHECK

Trace the paths through the program and make sure every OPEN is matched by exactly one CLOSE.

TRY IT ▸

MainframeKit assistant

What it means

A cursor can only be opened once until it is closed. A second OPEN on an open cursor gets -502 with SQLSTATE 24502. Db2 does not reopen or reposition the cursor; it stays open at its current position.

This is a program logic problem, not a data or Db2 system problem.

Typical causes

  • A loop that opens the cursor on each pass but closes it only on some paths.
  • An early exit from the fetch loop (for example on an error or a limit) that skips the CLOSE.
  • A cursor declared WITH HOLD that stays open across a COMMIT, then opened again.
  • A subroutine or paragraph called twice, each time opening the same cursor.

How to fix it

  1. Trace the paths through the program and make sure every OPEN is matched by exactly one CLOSE.
  2. Close the cursor on every exit from the fetch loop, including error exits.
  3. If the logic genuinely may find the cursor open, CLOSE it first and treat -501 on that CLOSE as harmless.
  4. Remember WITH HOLD cursors survive COMMIT; do not reopen them after the commit.