Figure 11-13 Declaration of sqlca in the sqlca.h Header File
Figure 11-14 illustrates the fields of the sqlca structure.
Figure 11-14 Fields of the sqlca Structure
Using the SQLCODE Variable The SQLCODE variable is a long integer 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: 1. The database server stores the exception value in the sqlcode field of the sqlca structure.
2. ESQL/C copies the value of sqlca.sqlcode to the global SQLCODE variable.
SQLCODE = 0
0
Success
SQLCODE = 100
100
NOT FOUND condition
SQLCODE < 0
Runtime error
For information about the values of SQLCODE (and sqlca.sqlcode) and their corrective actions, refer to the Informix Error Messages manual. For information about how to handle these exceptions, see "Checking for Exceptions with sqlca".
The following sections provide additional information about SQLCODE. SQLCODE in Pure C Modules 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:
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. Checking for Exceptions with sqlca After an SQL statement executes, the sqlca structure can indicate one of the four possible conditions that Figure 11-15 shows.
Figure 11-15 Exceptions That the sqlca Structure Returns Exception Condition sqlca Value Success SQLCODE (and sqlca.sqlcode) = 0 Success, but no rows found SQLCODE (and sqlca.sqlcode) = 100 Success, but warnings generated sqlca.sqlwarn.sqlwarn0 = 'W' To indicate specific warning: One of sqlwarn1 to sqlwarn7 in the sqlca.sqlwarn structure is also set to W Failure, runtime error generated SQLCODE (and sqlca.sqlcode) < 0 For a general introduction to these four conditions, see "Types of Diagnostic Information". The following sections discuss how sqlca indicates each condition.
SQLCODE (and sqlca.sqlcode) = 0
Success, but no rows found
SQLCODE (and sqlca.sqlcode) = 100
Success, but warnings generated
sqlca.sqlwarn.sqlwarn0 = 'W'
'W'
To indicate specific warning:
W
Failure, runtime error generated
SQLCODE (and sqlca.sqlcode) < 0
Success in sqlca 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:
NOT FOUND in SQLCODE 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 SQLCODE Values That Are Set When SQL Statements Do Not Return Any Rows (1 of 2) SQL Statement Where SQLCODE Gets the Indicated Result Result for ANSI-Compliant Database Result for Non-ANSI-Compliant Database FETCH statement: the last qualifying row has already been returned (the end of data has been reached). 100 100 SELECT statement: no rows match the SELECT criteria. 100 100 DELETE and DELETE...WHERE statement (not part of multistatement PREPARE): no rows match the DELETE criteria. 100 0 INSERT INTO tablename SELECT statement (not part of multistatement PREPARE): no rows match the SELECT criteria. 100 0 SELECT... INTO TEMP statement (not part of multistatement PREPARE): no rows match the SELECT criteria. 100 0 UPDATE...WHERE statement (not part of multistatement PREPARE): no rows match the UPDATE criteria. 100 0 Figure 11-16 shows that what the NOT FOUND condition generates depends, in some cases, on whether the database is ANSI compliant.
(1 of 2)
FETCH statement: the last qualifying row has already been returned (the end of data has been reached).
SELECT statement: no rows match the SELECT criteria.
DELETE and DELETE...WHERE statement (not part of multistatement PREPARE): no rows match the DELETE criteria.
INSERT INTO tablename SELECT statement (not part of multistatement PREPARE): no rows match the SELECT criteria.
SELECT... INTO TEMP statement (not part of multistatement PREPARE): no rows match the SELECT criteria.
UPDATE...WHERE statement (not part of multistatement PREPARE): no rows match the UPDATE criteria.
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.
The second sqlwarn table in Figure 11-14 lists warnings that other SQL statements might generate.
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 block of code that Figure 11-17 shows.
Figure 11-17 Code Fragment That Checks for Warnings After a CONNECT Statement
Runtime Errors in SQLCODE 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 Informix Error Messages manual lists these Informix-specific error codes and their corrective actions. You can also use the command-line finderr utility to obtain information about an Informix error. For more information about finderr, see the Informix Error Messages manual. 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:
?,
?
:x,
:y,
:z
)
HELP_CONTEXT
Error number from SQLCODE or sqlca.sqlcode
HELP_CONTEXTPOPUP
HELP_KEY
Pointer to string that contains error number from SQLCODE or sqlca.sqlcode and is converted to ASCII with sprintf() or wsprintf()
HELP_PARTIALKEY