SQL/MX 3.2 Programming Manual for C and COBOL (H06.25+, J06.14+)
Exception Handling and Error Conditions
HP NonStop SQL/MX Release 3.2 Programming Manual for C and COBOL—663854-002
13-16
Declaring SQLSTATE or SQLCODE in an Error
Routine
FETCH and SQLCODE is checked, the value is equal to the original value returned
from FETCH.
Example
This example uses WHENEVER SQLERROR PERFORM to save and restore
SQLSTATE:
EXEC SQL BEGIN DECLARE SECTION END-EXEC.
01 sqlstate pic x(5).
01 saved-sqlstate pic x(5).
01 hv-num pic s9(9) comp.
...
EXEC SQL END DECLARE SECTION END-EXEC.
...
EXEC SQL WHENEVER SQLERROR PERFORM sqlerrors END-EXEC.
...
sqlerrors.
MOVE sqlstate TO saved-sqlstate.
EXEC SQL WHENEVER SQLERROR CONTINUE END-EXEC.
EXEC SQL GET DIAGNOSTICS
:hv-num = NUMBER
END-EXEC.
PERFORM VARYING i FROM 1 BY 1 UNTIL i > hv-num
MOVE SPACES TO hv-msgtxt
EXEC SQL GET DIAGNOSTICS EXCEPTION :i
:hv-sqlstate = RETURNED_SQLSTATE,
:hv-msgtxt = MESSAGE_TEXT
END-EXEC.
DISPLAY "SQLSTATE: " hv-sqlstate
DISPLAY "Message : " hv-msgtxt
END-PERFORM.
MOVE saved-sqlstate TO sqlstate.
...
END PROGRAM Program-exF72.
For information on using the GET DIAGNOSTICS Statement, see Accessing and Using
the Diagnostics Area on page 13-17.
Declaring SQLSTATE or SQLCODE in an Error Routine
As described in Saving and Restoring SQLSTATE or SQLCODE on page 13-15, you
can save and restore SQLSTATE or SQLCODE within the SQL error function.
Alternately, in a C program, you can declare SQLSTATE in the function (in addition to
declaring SQLSTATE in the main routine).
Example
This example program has two SQL Declare Sections, both of which contain an
SQLSTATE declaration:
void sql_error(void);
...
int main ()
{
COBOL
C










