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.

  1. A SCROLL CURSOR is used to allow the user to scroll through a result set generated by a query. The scroll cursor is declared WITH HOLD so it will not be closed when a COMMIT WORK or ROLLBACK WORK is executed.
  2. When the user chooses Update, the values in the current program record are copied to the work record.
  3. The INPUT statement accepts the user's input and stores it in the program record. The WITHOUT DEFAULTS keywords are used to insure that the original values retrieved from the database were not replaced with default values.
  4. If the user accepts the input, a transaction is started with BEGIN WORK.
  5. The primary key stored in the program record is used to SELECT the same row into the local record. FOR UPDATE locks the row.
  6. The SQLCA.SQLCODE is checked, in case the database row was deleted after the initial query.
  7. The work record and the local record are compared, in case the database row was changed after the initial query.
  8. If the work and local records are identical, the database row is updated using the new program record values input by the user.
  9. If the UPDATE is successful, a COMMIT WORK is issued. Otherwise, a ROLLBACK WORK is issued.
  10. The SCROLL CURSOR has remained open, allowing the user to continue to scroll through the query result set.