Db2 Troubleshooting
SQLCODE -407 null inserted into non-null column
SQLCODE -407: null inserted into a NOT NULL column — the program tried to store null where nulls are forbidden.
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
- Indicator variable set to null for a NOT NULL column.
- Missing value defaulted to null.
How to diagnose
- Check which column is NOT NULL and what the program passed.
- Inspect indicator variables.
How to fix it
- Supply a real value or a DEFAULT.
- Fix the program logic that leaves the value unset.
Example: Supply values for NOT NULL columns
IF WS-SAL-IND < 0
MOVE 0 TO WS-SAL *> default instead of null
END-IF.
Defaults beat nulls for NOT NULL columns.
Interview tip
- Q: How would you troubleshoot a SQLCODE -407 null inserted into non-null column 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.
