SQL/MP Programming Manual for COBOL

Data Retrieval and Modification
HP NonStop SQL/MP Programming Manual for COBOL529758-003
4-9
Updating a Single Row
SQL/MP returns these values to SQLCODE after an UPDATE statement.
The UPDATE statement updates rows in sequence. If an error occurs, SQL/MP returns
an error code to SQLCODE and terminates the UPDATE operation. The SQLCA
structure contains the number of rows updated. (If the UPDATE statement fails, do not
rely on the SQLCA structure for an accurate count of the number of rows updated.) To
return the contents of the SQLCA structure, use the SQLCA_DISPLAY2_ or
SQLCA_TOBUFFER2_ procedure.
For more information, see Section 5, SQL/MP System Procedures, and Section 9,
Error and Status Reporting.
Updating a Single Row
This example updates a single row of the ORDERS table that contains information
about the order number specified by UPDATE-ORDERNUM. In a typical application, a
user enters the values for UPDATE-DATE and UPDATE-ORDERNUM. If the UPDATE
operation fails, check for SQL error 8227, which indicates you attempted to update a
row with an existing key (primary or unique alternate).
EXEC SQL BEGIN DECLARE SECTION END-EXEC.
01 ORDERS.
02 ORDERNUM PIC 9(6) DISPLAY.
02 ORDER-DATE PIC 9(6) DISPLAY
02 DELIV-DATE PIC 9(6) DISPLAY.
02 SALESREP PIC 9(4) DISPLAY.
02 CUSTNUM PIC 9(4) DISPLAY.
01 NEWDATE PIC 9(6) DISPLAY.
EXEC SQL END DECLARE SECTION END-EXEC.
...
PROCEDURE DIVISION.
...
MOVE UPDATE-DATE TO NEWDATE.
MOVE UPDATE-ORDERNUM TO ORDERNUM OF ORDERS.
EXEC SQL UPDATE ORDERS SET DELIV-DATE = :NEWDATE
WHERE ORDERNUM = :ORDERNUM OF ORDERS
STABLE ACCESS END-EXEC.
Updating Multiple Rows
If you do not need to check a value in a row before you update the row, use a single
UPDATE statement to update multiple rows in a table. This example updates the
SALARY column of all rows in the EMPLOYEE table where the SALARY value is less
SQLCODE Value Description
0
The UPDATE statement was successful.
100
No rows were found on a search condition.
< 0
An error occurred; SQLCODE contains the error number.
> 0 (not 100)
A warning occurred; SQLCODE contains the first warning number.