SQL/MX Programming Manual for C and COBOL (G06.24+, H06.03+)

Exception Handling and Error Conditions
HP NonStop SQL/MX Programming Manual for C and COBOL523627-004
13-8
Accessing SQL Messages Within a Program
SQL 1000
Cause. Syntax was entered incorrectly.
Effect. SQL is unable to prepare the statement.
Recovery. Correct the syntax error and resubmit.
The second number, if present, is the corresponding SQL:1999 SQLSTATE value. In
this example, SQLSTATE 42000 is an ANSI value.
Within MXCI, you can display text associated with a message number or SQLCODE
value by using the ERROR command. For further information, see the ERROR
Command in the SQL/MX Reference Manual.
Accessing SQL Messages Within a Program
To obtain error messages that result from the execution of an SQL statement within a
program, use the GET DIAGNOSTICS statement to access the SQL diagnostics area.
For example, you might code your program to display:
SQLSTATE: 22001
SQLCODE : -8402
Message : *** ERROR[8402] A string overflow occurred during
the evaluation of a character expression.
The corresponding fields in the diagnostics area are RETURNED_SQLSTATE,
SQLCODE, and MESSAGE_TEXT, respectively. The message text also provides the
SQL message number.
See Accessing and Using the Diagnostics Area on page 13-13.
Using the WHENEVER Statement
The WHENEVER declaration specifies an action that a program takes, depending on
the results of subsequent SQL statements. When you specify WHENEVER, the
SQL/MX preprocessor generates statements in your program that perform run-time
checking using the SQLSTATE variable after each SQL statement executes.
The generated statements check for these conditions:
NOT FOUND condition: No data was found. SQLSTATE is 02000, and SQLCODE
is 100.
SQLERROR condition: An SQL error occurred. SQLSTATE indicates an exception
condition as shown in Checking the SQLSTATE Variable on page 13-1. SQLCODE
is less than zero.
1000 42000 A syntax error occurred.