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-4
Using SQLSTATE
In Table 13-2, for the last two cases, the subclass abbreviated yzz is in one of the
following ranges: W00 through W09, W0A through WZZ, X00 through X09, or X0A
through XZZ.
Using SQLSTATE
After you declare SQLSTATE within a Declare Section, use conditional statements to
check the SQLSTATE variable after the execution of an SQL statement.
This example checks the SQLSTATE variable for errors or warnings after an UPDATE
statement. SQLSTATE is declared as global so that it can be referenced in the
process_sqlstate function:
Example
void process_sqlstate(void);
EXEC SQL BEGIN DECLARE SECTION;
char SQLSTATE[6];
...
EXEC SQL END DECLARE SECTION;
...
int main()
{
char SQLSTATE_OK[6]="00000";
SQLSTATE[5]='\0';
SQLSTATE_OK[5]='\0';
...
EXEC SQL BEGIN WORK;
EXEC SQL UPDATE customer SET CREDIT = 'CR';
if (strcmp(SQLSTATE, SQLSTATE_OK) == 0) {
printf ("\nRows were updated!");
EXEC SQL COMMIT WORK;
} else process_sqlstate();
...
return 0;
} /* end main */
void process_sqlstate(void)
{
printf("\nError or warning occurred! SQLSTATE = %s",SQLSTATE);
... /* Process the SQL error. */
} /* end process_sqlstate */
100 02000 ISO 9075 ISO 9075 No data
n < 0 X0yzz SQL/MX SQL/MX Error
n > 0 (<> 100) 01yzz ISO 9075 SQL/MX Warning
Table 13-2. Mapping of SQLCODE to SQL/MX-Defined SQLSTATE Values
SQLCODE SQLSTATE
Class
Origin
Subclass
Origin Description
C