SQLCA


The SQL Communication Area (SQLCA) is a data structure used in DB2 programs (like COBOL) to capture the result and status of SQL operations. After each SQL statement executes, DB2 populates the SQLCA fields with information about the result—whether the SQL statement succeeded, failed, or returned a warning.

The SQLCA should be included in a COBOL program to check for SQL errors or warnings after executing SQL queries.

Coding in COBOL + DB2 program -

The SQLCA contains several fields that store detailed information about the result of the SQL statement. The INCLUDE SQLCA statement pulls in the SQLCA structure to our program.

EXEC SQL 
	INCLUDE SQLCA 
END-EXEC.
Note! The EXEC SQL and END-EXEC keywords are used to embed SQL statements within COBOL code. All SQL statements like SELECT, INSERT, UPDATE, DELETE, COMMIT, SQLCA and table DCLGENs are enclosed between these keywords.

Key Fields of SQLCA and Their Uses -

Field NameTypeDescription
SQLCAIDCHAR(8)Identifier for the SQLCA structure. After including SQLCA, this field will always have 'SQLCA'.
SQLCABCINTEGERLength of the SQLCA structure (136 bytes)
SQLCODEINTEGERReturn code indicating the result of the SQL operation.
  • 0: Successful operation.
  • 100: No rows found (common with SELECT statements).
  • Negative: An error occurred (e.g., -803 for duplicate key).
SQLERRMCHAR(70)This field stores a short error message related to the SQLCODE.
SQLERRD (Array)INTEGER Array of 6 itemsContains DB2-specific diagnostic information.
  • SQLERRD(1): Rows affected by the operation (INSERT, UPDATE, DELETE).
  • SQLERRD(3): Returns the SQL processing time for the query (performance metrics).
  • SQLERRD(5): Contains the row number where an error occurred.
SQLWARN (Array)CHAR(1) of 11 itemsProvides warning flags if the SQL operation completed with warnings.
  • SQLWARN0: Set to 'W' if any other SQLWARN flag is set.
  • SQLWARN1: Set to 'W' if a value was truncated.
  • SQLWARN2: Set to 'W' if NULL values were eliminated from the result.
SQLSTATECHAR(5)Provides a standardized return code for SQL operations. SQLSTATE codes give more portable error messages across different databases.
  • '00000': Successful operation.
  • '02000' No data found.
  • '23505' Unique constraint violation (duplicate key).

All fields in SQLCA -

NameData Type
SQLCAIDCHAR(8)
SQLCABCINTEGER
SQLCODEINTEGER
SQLERRMLSMALLINT
SQLERRMCVARCHAR (70)
SQLERRPCHAR(8)
SQLERRDARRAY
SQLERRD(1)INTEGER
SQLERRD(2)INTEGER
SQLERRD(3)INTEGER
SQLERRD(4)INTEGER
SQLERRD(5)INTEGER
SQLERRD(6)INTEGER
SQLWARN0CHAR(1)
SQLWARN1CHAR(1)
SQLWARN2CHAR(1)
SQLWARN3CHAR(1)
SQLWARN4CHAR(1)
SQLWARN5CHAR(1)
SQLWARN6CHAR(1)
SQLWARN7CHAR(1)
SQLWARN8CHAR(1)
SQLWARN9CHAR(1)
SQLWARN10CHAR(1)
SQLSTATECHAR(5)