Define the SQL communications area in C and C++ applications that use SQL

A C or C++ program can be written to use the SQLCA to check return status for embedded SQL statements, or the program can use the SQL diagnostics area to check return status.

When using the SQLCA, a C or C++ program that contains SQL statements must include one or both of the following:

Or,

The SQLCODE and SQLSTATE values are set by the database manager after each SQL statement is run. An application can check the SQLCODE or SQLSTATE value to determine whether the last SQL statement was successful.

Start of changeYou can code the SQLCA in a C or C++ program directly or by using the SQL INCLUDE statement. When coding it directly, initialize the SQLCA using the following statement: End of change

Start of change
struct sqlca sqlca = {0x0000000000000000};
End of change

Using the SQL INCLUDE statement requests the inclusion of a standard declaration:

  EXEC SQL INCLUDE SQLCA ;

Start of changeA standard declaration includes a structure definition and a data area that are named sqlca.End of change

The SQLCODE, SQLSTATE, and SQLCA variables must appear before any executable statements. The scope of the declaration must include the scope of all SQL statements in the program.

The included C and C++ source statements for the SQLCA are:

  #ifndef SQLCODE
  struct sqlca {
                unsigned char sqlcaid[8];
                long          sqlcabc;
                long          sqlcode;
                short         sqlerrml;
                unsigned char sqlerrmc[70];
                unsigned char sqlerrp[8];
                long          sqlerrd[6];
                unsigned char sqlwarn[11];
                unsigned char sqlstate[5];
               };
  #define SQLCODE sqlca.sqlcode
  #define SQLWARN0 sqlca.sqlwarn[0]
  #define SQLWARN1 sqlca.sqlwarn[1]
  #define SQLWARN2 sqlca.sqlwarn[2]
  #define SQLWARN3 sqlca.sqlwarn[3]
  #define SQLWARN4 sqlca.sqlwarn[4]
  #define SQLWARN5 sqlca.sqlwarn[5]
  #define SQLWARN6 sqlca.sqlwarn[6]
  #define SQLWARN7 sqlca.sqlwarn[7]
  #define SQLWARN8 sqlca.sqlwarn[8]
  #define SQLWARN9 sqlca.sqlwarn[9]
  #define SQLWARNA sqlca.sqlwarn[10]
  #define SQLSTATE sqlca.sqlstate
  #endif
  struct sqlca sqlca = {0x0000000000000000};
When a declare for SQLCODE is found in the program and the precompiler provides the SQLCA, SQLCADE replaces SQLCODE. When a declare for SQLSTATE is found in the program and the precompiler provides the SQLCA, SQLSTOTE replaces SQLSTATE.
Note: Many SQL error messages contain message data that is of varying length. The lengths of these data fields are embedded in the value of the SQLCA sqlerrmc field. Because of these lengths, printing the value of sqlerrmc from a C or C++ program might give unpredictable results.
Related concepts
Use the SQL diagnostics area
Related information
SQL Communication Area
GET DIAGNOSTICS