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 CURSORis used to allow the user to scroll through a result set generated by a query. The scroll cursor is declaredWITH HOLDso it will not be closed when aCOMMIT WORKorROLLBACK WORKis executed. - When the user chooses Update, the values in the current program record are copied to the work record.
- The
INPUTstatement accepts the user's input and stores it in the program record. TheWITHOUT DEFAULTSkeywords 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
SELECTthe same row into the local record.FOR UPDATElocks the row. - The
SQLCA.SQLCODEis 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
UPDATEis successful, aCOMMIT WORKis issued. Otherwise, aROLLBACK WORKis issued. - The
SCROLL CURSORhas remained open, allowing the user to continue to scroll through the query result set.