SQLCODE -503: Column not in the cursor's FOR UPDATE list
SQLCODE -503 (SQLSTATE 42912) means an UPDATE ... WHERE CURRENT OF tries to change a column that isn't named in the FOR UPDATE OF clause of the cursor's declaration. Add the column to that list or remove it from the UPDATE.
Other names for this code
Also written as SQL -503, SQLCODE=-503, SQLCODE = -503, db2 -503, sqlstate 42912, SQL0503N, db2 column not identified in for update clause, SQL0503.
What it means
A positioned update changes the row the cursor is on. Db2 only allows it for columns the cursor said it would update. When the DECLARE CURSOR ends with FOR UPDATE OF COL1, COL2, those are the only columns an UPDATE ... WHERE CURRENT OF on that cursor may set. Setting any other column gives -503.
For static SQL the check usually happens when the program is precompiled or bound, so you find out before it runs. For dynamic SQL it happens when the positioned UPDATE is prepared.
On Db2 for z/OS, FOR UPDATE with no column list allows updates to any column that can be updated. The NOFOR precompiler option goes further and makes the FOR UPDATE clause optional for static cursors.
Common causes
- A column was added to the UPDATE ... WHERE CURRENT OF statement but not to the cursor's FOR UPDATE OF list.
- The UPDATE was copied from another program whose cursor lists different columns.
- The cursor lists only a few columns on purpose, and a later change needed one more.
- Two cursors over the same table have similar names, and the UPDATE refers to the wrong one.
How to find the problem
- Read the precompile or bind listing. It points to the UPDATE statement.
- Compare the SET list of the UPDATE with the FOR UPDATE OF list of the cursor named in WHERE CURRENT OF.
- Check that the cursor named in WHERE CURRENT OF is the one you meant.
How to fix it
- Add the column to the cursor's FOR UPDATE OF list, then precompile and bind again.
- Or change the clause to plain FOR UPDATE so every updatable column is allowed.
- Or update the row with a searched UPDATE using its key, if a positioned update isn't needed.
- Keep the FOR UPDATE OF list and the positioned UPDATE close together in the source, so changes to one are made to the other.
IBM documentation
IBM documentation: -503. Use the documentation for your installed release.
Related codes
Questions
Why list columns in FOR UPDATE OF at all?
It documents what the program changes and limits positioned updates to those columns. Plain FOR UPDATE is simpler if you update many columns.
Does FOR UPDATE change locking?
Yes. On Db2 for z/OS a cursor declared FOR UPDATE takes update (U) locks on the rows it fetches, which reduces deadlocks when the program then updates them.
Is it the same on Db2 for LUW?
Yes. Db2 for LUW reports SQL0503N with SQLSTATE 42912.