Using a work record
A work record and a local record, both identical to the program record, are defined to allow the program to compare the values.
- A
SCROLL CURSOR
is used to allow the user to scroll through a result set generated by a query. The scroll cursor is declaredWITH HOLD
so it will not be closed when aCOMMIT WORK
orROLLBACK WORK
is executed. - When the user chooses Update, the values in the current program record are copied to the work record.
- The
INPUT
statement accepts the user's input and stores it in the program record. TheWITHOUT DEFAULTS
keywords are used to insure that the original values retrieved from the database were not replaced with default values. - If the user accepts the input, a transaction is started with
BEGIN WORK
. - The primary key stored in the program record is used to
SELECT
the same row into the local record.FOR UPDATE
locks the row. - The
SQLCA.SQLCODE
is checked, in case the database row was deleted after the initial query. - The work record and the local record are compared, in case the database row was changed after the initial query.
- If the work and local records are identical, the database row is updated using the new program record values input by the user.
- If the
UPDATE
is successful, aCOMMIT WORK
is issued. Otherwise, aROLLBACK WORK
is issued. - The
SCROLL CURSOR
has remained open, allowing the user to continue to scroll through the query result set.