Master Mainframe Technologies - COBOL, JCL, DB2, VSAM, CICS & More
ABEND Codes SQLCODEs File Status Interview Prep Contact
🗃 DB2

DB2 Error Handling

Intermediate 🕑 15 min read 👁 0 views

15

Code Example


## DB2 Error Handling

Proper error handling ensures robust DB2 applications.

### SQLCODE
| Value | Meaning |
|-------|---------|
| 0 | Success |
| 100 | No rows found |
| -803 | Duplicate key |
| -530 | RI violation |
| -811 | Multiple rows |
| -904 | Resource unavailable |

### SQLSTATE
5-character standard code:
- '00000' = Success
- '02000' = No data
- '23xxx' = Constraint violation

### WHENEVER Statement
\`\`\`sql
EXEC SQL WHENEVER SQLERROR GOTO error-para END-EXEC
EXEC SQL WHENEVER NOT FOUND CONTINUE END-EXEC
\`\`\`

### Error Information
- SQLERRMC: Error message
- SQLERRD: Error details array

### Best Practices
1. Always check SQLCODE
2. Log error details
3. Use appropriate rollback
4. Provide meaningful messages