DB2 Server for VSE & VM: Application Programming


Handling SQL Errors

A return code structure (the SQLCA) must be in scope for each executable SQL statement. You can define one by coding the following statement in your source program:

   EXEC SQL INCLUDE SQLCA;

The preprocessor replaces this statement with the declaration of the SQLCA structure, and a set of #defines to make referring to the error codes and flags easier. These are shown in Figure 113.

Figure 113. SQLCA Structure (in C)

#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;
Note:SQLCA character array variables are not NUL-terminated. They cannot be directly used by C string manipulation functions.

The SQLCA must not be declared within the SQL declare section. It may be declared outside all functions in the module, which gives it global scope, or separately within each function that contains executable SQL statements.

Instead of using the SQL INCLUDE SQLCA statement, the SQLCA can be coded directly, or #included from a header file.

You may find that the only variable in the SQLCA that you really need is SQLCODE. If this is the case, declare just the SQLCODE variable, and invoke NOSQLCA support at preprocessor time.

The number of SQLCODE declarations is not limited by the DB2 Server for VSE & VM preprocessor. If a stand-alone SQLCODE is specified, the code inserted by the preprocessor into the C code to expand an EXEC SQL statement will refer to the address of that SQLCODE. The C compiler determines if multiple declarations within a program section are not acceptable. In addition, the C compiler determines which region of the code an SQLCODE declaration refers to.


[ Top of Page | Previous Page | Next Page | Table of Contents | Index ]