![]() |
|
An alternative way to obtain diagnostic information is through the SQL Communications Area. When an SQL statement executes, the database server automatically returns information about the success or failure of the statement in a C structure that is called sqlca. To obtain exception information, your ESQL/C program can access the sqlca structure or the SQLCODE variable as follows:
Important: ESQL/C supports the sqlca structure for backward compatibility. Informix recommends, however, that new applications use the SQLSTATE variable with the GET DIAGNOSTICS statement to perform exception checking. This method conforms to X/Open and ANSI SQL standards and supports multiple exceptions.
The next three sections describe how to use the SQLCODE variable and the sqlca structure to perform exception handling. These sections cover the following topics:
Figure 11-13 contains the declaration of the sqlca structure from the sqlca.h header file. The ESQL/C preprocessor automatically includes the sqlca.h header file in an ESQL/C program.
Figure 11-14 illustrates the fields of the sqlca structure.
Figure 11-14
The SQLCODE variable is an int4 that indicates whether the SQL statement succeeded or failed. The ESQL/C header file, sqlca.h, declares SQLCODE as a global variable. Since the ESQL/C preprocessor automatically includes sqlca.h in an ESQL/C program, you do not need to declare SQLCODE.
When the database server executes an SQL statement, the database server automatically updates the SQLCODE variable as follows:
Tip: For readability and brevity, use SQLCODE in your ESQL/C program in place of sqlca.sqlcode.
The SQLCODE value can indicate the following types of exceptions:
For information about the values of SQLCODE (and sqlca.sqlcode) and their corrective actions, use the finderr or Find Error utility or view Informix Error Messages in Answers OnLine. For information about how to handle these exceptions, see Checking for Exceptions with sqlca.
The following sections provide additional information about SQLCODE.
To return the same values that the SQLCODE status variable in ESQL/C modules returns, you can use SQLCODE in pure C modules (modules with the .c extension) that you link to an ESQL/C program. To use SQLCODE in a pure C module, declare SQLCODE as an external variable, as follows:
To return an error code to a parent process, do not attempt to use the SQLCODE value as an argument to the exit() system call. When ESQL/C passes back the argument of exit() to the parent, it passes only the lower eight bits of the value. Since SQLCODE is a four-byte (long) integer, the value that ESQL/C returns to the parent process might not be what you expect.
To pass error information between processes, use the exit value as an indication that some type of error has occurred. To obtain information on the actual error, use a temporary file, a database table, or some form of interprocess communication.
The DESCRIBE statement returns information about a prepared statement before the statement executes. It operates on a statement ID that a PREPARE statement has previously assigned to a dynamic SQL statement.
After a successful DESCRIBE statement, the database server sets SQLCODE (and sqlca.sqlcode) to a nonnegative integer value that represents the type of SQL statement that DESCRIBE has examined. The sqlstype.h header file declares constant names for each of these return values. For a list of possible SQLCODE values after a DESCRIBE statement, see Determining the Statement Type.
Because the DESCRIBE statement uses the SQLCODE field differently than any other statement, you might want to revise your exception-handling routines to accommodate this difference.
After an SQL statement executes, the sqlca structure can indicate one of the four possible conditions that Figure 11-15 shows.
Figure 11-15
For a general introduction to these four conditions, see Types of Database Exceptions. The following sections discuss how sqlca indicates each condition.
When the database server executes an SQL statement successfully, it sets SQLCODE (sqlca.sqlcode) to zero (0). The database server might also set one or more of the following informational fields in sqlca after a successful SQL statement:
For more information on these additional fields, see Fields of the sqlca Structure. In addition, the SQLCODE value for some SQL statements has special meaning. For more information, see Using the SQLCODE Variable.
When a SELECT or FETCH statement encounters NOT FOUND (or END OF DATA), the database server sets SQLCODE (sqlca.sqlcode) to 100. Figure 11-16 lists conditions that cause SQL statements to yield NOT FOUND.
Figure 11-16
Figure 11-16 on page 11-35 shows that what the NOT FOUND condition generates depends, in some cases, on whether the database is ANSI compliant.
In the following example, the INSERT statement inserts into the hot_items table any stock item that has an order quantity greater than 10,000. If no items have an order quantity that great, the SELECT part of the statement fails to insert any rows. The database server returns 100 in an ANSI-compliant database and 0 if the database is not ANSI compliant.
For readability, use the constant SQLNOTFOUND for the END OF DATA value of 100. The sqlca.h header file defines the SQLNOTFOUND constant. The following comparison checks for the NOT FOUND and END OF DATA conditions:
When the database server executes an SQL statement successfully, but encounters a warning condition, it updates the following two fields in the sqlca.sqlwarn structure:
These warnings are Informix specific. Figure 11-14 on page 11-29 contains two sets of warning conditions that can occur in the fields of the sqlca.sqlwarn structure. The first set of warnings, shown in Figure 11-14, occur after the database server opens a database or establishes a connection. For more information on these conditions, see Determining Features of the Database Server. The second set of warnings are for conditions that can occur as a result of other SQL statements.
To test for warnings, check whether the first warning field (sqlwarn0) is set to W. Once you determine that the database server has generated a warning, you can check the values of the other fields in sqlca.sqlwarn to identify the specific condition. For example, if you want to find out what kind of database a CONNECT statement has opened, you can use the code that Figure 11-17 shows.
When an SQL statement results in a runtime error, the database server sets SQLCODE (and sqlca.sqlcode) to a negative value. The actual number identifies the particular error. The error message documentation lists the Informix-specific error codes and their corrective actions. To see the error-message documentation, use the finderr or Find Error utility or view Informix Error Messages in Answers OnLine.
From within your ESQL/C program, you can retrieve error message text that is associated with a negative SQLCODE (sqlca.sqlcode) value with the rgetlmsg() or rgetmsg() library function. See Library Functions for Retrieving Error Messages.
When the database server encounters a runtime error, it might also set the following other fields in the sqlca structure:
Tip: You can also test for errors with the WHENEVER SQLERROR statement. For more information, see The WHENEVER Statement.
When the database server returns an error for a PREPARE statement, this error is usually because of a syntax error in the prepared text. When this occurs, the database server returns the following information:
If you prepare multiple statements with a single PREPARE statement, the database server returns an error status on the first error in the text, even if it encounters several errors.
Important: The sqlerrd[4] field, which is the offset of the error into the SQL statement, might not always be correct because the ESQL/C preprocessor converts the embedded SQL statements into host-language format. In so doing, the preprocessor might change the relative positions of the elements within the embedded statement.
For example, consider the following statement, which contains an invalid WHERE clause:
The preprocessor converts this statement to a string like the following string:
This string does not have the EXEC SQL keywords. Also, the characters ?, ?, ? have replaced :x, :y, :z (five characters instead of eight). The ESQL/C preprocessor has also dropped a newline character between the left parenthesis (")") and the WHERE keyword. Thus, the offset of error in the SQL statement that the database server sees is different than the offset of the error in the embedded SQL statement.
The sqlca.sqlerrd[4] field also reports statement-offset values for errors in the EXECUTE IMMEDIATE and DECLARE statements.
SQLCODE After an EXECUTE StatementAfter an EXECUTE statement, the database server sets SQLCODE to indicate the success of the prepared statement as follows:
Your ESQL/C application can use the Informix ERRMESS.HLP file to display text that describes an error and its corrective action. You can call the Windows API WinHelp() with the following WinHelp parameters.