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