Db2 Troubleshooting
SQLCODE -803 duplicate key
SQLCODE -803: duplicate key — INSERT hit an existing unique key value.
What it means
- After every EXEC SQL, DB2 returns a SQLCODE: 0 means success, +100 means no row found, and negative values mean an error.
Common causes
- Program inserts a row whose key already exists.
- Restart reprocessing inserted rows twice.
How to diagnose
- Identify the duplicate key value from SQLERRM.
- Check whether the row genuinely exists already.
How to fix it
- Skip existing rows, or change to UPDATE logic.
- Fix restart logic so inserts are not repeated.
Example: Make inserts idempotent
* Check existence before insert, or handle -803 as 'already there'.
-803 often means the work is already done.
Interview tip
- Q: How would you troubleshoot a SQLCODE -803 duplicate key in production?
- A: I would check the SQLCODE and SQLERRM immediately after the failing statement, reproduce in SPUFI, then apply the specific fix.
Prevention checklist
- Always check SQLCODE after every EXEC SQL statement.
- Log the SQLCODE, SQLSTATE and failing statement for support.
