SQL/MP Programming Manual for COBOL

Error and Status Reporting
HP NonStop SQL/MP Programming Manual for COBOL529758-003
9-5
Checking the SQLCODE Identifier
Using the SQLCODE Data Item
The HP COBOL compiler does not automatically generate an SQLCA structure, which
includes the SQLCODE data item. You must declare an SQLCODE identifier either
explicitly as a PIC S9(4) COMP data item or implicitly with an INCLUDE SQLCA
directive. You cannot specify SQLCODE both explicitly as a data item and implicitly
with an INCLUDE SQLCA directive.
This example uses the INCLUDE SQLCA directive to implicitly declare the SQLCODE
data item:
WORKING-STORAGE SECTION.
EXEC SQL BEGIN DECLARE SECTION END-EXEC.
01 IN-PARTS-REC.
10 IN-PARTNUM PIC 9(4).
10 IN-PRICE PIC 9(6)V99.
10 IN-PARTDESC PIC X(18).
EXEC SQL END DECLARE SECTION END-EXEC.
EXEC SQL INCLUDE SQLCA END-EXEC.
...
This example checks SQLCODE only for errors and warnings:
PROCEDURE DIVISION.
...
300-INSERT-DATA.
MOVE 4120 TO IN-PARTNUM.
MOVE 60000.00 TO IN-PRICE.
MOVE "V8 DISK OPTION" TO IN-PARTDESC.
EXEC SQL
INSERT INTO SALES.PARTS (PARTNUM, PRICE, PARTDESC)
VALUES (:IN-PARTNUM, :IN-PRICE, :IN-PARTDESC)
END-EXEC.
* Check for errors and warnings
IF SQLCODE < 0 PERFORM 900-HANDLE-ERRORS.
IF SQLCODE > 0 AND
SQLCODE NOT = 100 PERFORM 910-HANDLE-WARNINGS.
...
This example checks for all values for SQLCODE:
PROCEDURE DIVISION.
...
IF SQLCODE OF SQLCA = 0
PERFORM DISPLAY-ROW-3000
ELSE
IF SQLCODE OF SQLCA = 100
PERFORM ROW-NOT-FOUND-7000
ELSE
IF SQLCODE OF SQLCA < 0
PERFORM SQL-ERROR-9000
ELSE
IF SQLCODE OF SQLCA > 0