Learn it on Mainframe Path. Do it with Mainframe KIT.Learn on Mainframe Path ▸
Mainframe KIT ● Nothing leaves this tab

-504

Cursor name is not declaredNEGATIVE SQLCODE · CHECKED 2026-11-14
Checked against IBM documentation

A statement refers to a cursor that has not been declared or allocated, or that was deallocated or is out of scope.

QUICK ANSWER

A statement refers to a cursor that has not been declared or allocated, or that was deallocated or is out of scope.

LIKELY CAUSES, IN THE ORDER OF THIS ENTRY

  1. The cursor was not declared or allocated before it was referenced.
  2. A positioned UPDATE or DELETE was used on an allocated cursor, which is not supported.
  3. A CLOSE or ROLLBACK deallocated the allocated cursor.
  4. A COMMIT deallocated it because the stored procedure's cursor was not WITH HOLD.
  5. The stored procedure was called again and returned new result sets.
  6. The declaration is not in scope for the reference.

CHECKED 14 Nov 2026 IBM: Db2 13 - Codes - SQLCODE -504 | Db2 for z/OS 13.0.0

MOST LIKELY

The cursor was not declared or allocated before it was referenced.

FIRST CHECK

Check the program or SQL routine for a missing or misspelled DECLARE or ALLOCATE CURSOR, placed before use.

TRY IT ▸

Site search

What it means

The cursor name was used before a DECLARE CURSOR or ALLOCATE CURSOR was in effect for it, or the cursor is no longer valid.

The statement is not processed.

Typical causes

  • The cursor was not declared or allocated before it was referenced.
  • A positioned UPDATE or DELETE was used on an allocated cursor, which is not supported.
  • A CLOSE or ROLLBACK deallocated the allocated cursor.
  • A COMMIT deallocated it because the stored procedure's cursor was not WITH HOLD.
  • The stored procedure was called again and returned new result sets.
  • The declaration is not in scope for the reference.

How to fix it

  1. Check the program or SQL routine for a missing or misspelled DECLARE or ALLOCATE CURSOR, placed before use.
  2. Call the stored procedure again and reissue ASSOCIATE LOCATORS and ALLOCATE CURSOR after a COMMIT, ROLLBACK or CLOSE.
  3. Declare the procedure's cursor WITH HOLD so COMMIT does not deallocate it.
  4. If the cursor name shows as <UNKNOWN>, check precompile warnings on the DECLARE or ALLOCATE statement.