Mainframe Path Start learning free
Core7 min readLesson 3 of 4

SQLCODEs and what to do about them

After every SQL statement, DB2 sets SQLCODE. Zero is success, +100 is 'no row found', anything negative is an error. Knowing eight of them covers most of what you will see.

SQLCODEMeaningUsual response
0Success
+100No row found, or end of cursorNormal — handle it explicitly
-180 / -181Invalid date or timestamp valueBad input data; validate before the call
-305Null returned with no indicator variableAdd a null indicator to the host variable
-501 / -502Cursor not open, or opened twiceFix the cursor logic
-803Duplicate key on insert or updateRow already exists; decide insert-or-update
-805Package not foundBind the package in this environment
-811More than one row returned by a singleton SELECTUse a cursor, or tighten the WHERE clause
-818Timestamp mismatch between load module and packageRecompile and rebind together
-904Resource unavailableObject is stopped or in a restricted state — check the reason code
-911 / -913Deadlock or timeout-911 rolled back; -913 did not. Retry logic belongs here
-922Authorisation failureSecurity: the plan, package, or table

The three that will actually cost you a night

A reusable SQL error handler
       9000-SQL-ERROR.
           DISPLAY 'SQL ERROR IN ' WS-PARAGRAPH
           DISPLAY '  SQLCODE  : ' SQLCODE
           DISPLAY '  SQLSTATE : ' SQLSTATE
           DISPLAY '  SQLERRMC : ' SQLERRMC
           CALL 'DSNTIAR' USING SQLCA, WS-ERR-MSG, WS-ERR-LEN
           PERFORM VARYING WS-I FROM 1 BY 1 UNTIL WS-I > 8
               DISPLAY WS-ERR-LINE(WS-I)
           END-PERFORM
           EXEC SQL ROLLBACK END-EXEC
           MOVE 16 TO RETURN-CODE
           GOBACK.

SQLSTATE alongside SQLCODE

SQLSTATE is a five-character standardised code that means the same thing across database products, where SQLCODE is DB2-specific. Log both: SQLCODE for looking things up in DB2 documentation, SQLSTATE for anything portable.

Common mistakes

Treating +100 as an error

Not finding a row is often the expected case. Handle it as a branch in your logic, not an abend.

Logging only the SQLCODE

SQLERRMC and DSNTIAR text name the object involved. Without them, an error is a number and nothing more.

Retrying a -913 without rolling back

-913 leaves your unit of work intact, so you must decide explicitly whether to roll back before retrying.

What you will see at work

Key terms

Check your understanding.
Take this lesson's quiz and save your progress. Free.

Take the lesson quiz
← Embedded SQL, precompile and BINDCommits, locking and why batch jobs collide →