UPDATE (Modify Existing Record)

Free-Form Syntax UPDATE{(E)} name {data-structure | %FIELDS(name{:name...})}
Code Factor 1 Factor 2 Result Field Indicators
UPDATE (E)   name (file or record format) data-structure _ ER _

The UPDATE operation modifies the last locked record retrieved for processing from an update disk file or subfile. No other operation should be performed on the file between the input operation that retrieved the record and the UPDATE operation.

The name operand must be the name of a file or record format to be updated. A record format name is required with an externally described file. The record format name must be the name of the last record read from the file; otherwise, an error occurs. A file name as the name operand is required with a program described file.

If the data-structure operand is specified, the record is updated directly from the data structure. The data structure must conform to the rules below:
  1. If the data-structure operand is specified, the record is updated directly from the data structure.
  2. If name refers to a program-described file, the data structure can be any data structure of the same length as the file's declared record length.
  3. If name refers to an externally-described file or a record format from an externally described database file, the data structure must be a data structure defined from the same file or record format, with *INPUT, *OUTPUT, or *ALL specified as the second parameter of the LIKEREC or EXTNAME keyword, or no second parameter specified for the LIKEREC keyword.
  4. If name refers to a subfile record format from an externally described display file, the data structure must be a data structure defined from the same file or record format, with *OUTPUT or *ALL specified as the second parameter of the LIKEREC or EXTNAME keyword.
  5. See File Operations for information on how to define the data structure and how data is transferred between the data structure and the file.

A list of the fields to update can be specified using %FIELDS. The parameter to %FIELDS is a list of the field names to update. See the example at the end of %FIELDS (Fields to update) for an illustration of updating fields.

To handle UPDATE exceptions (file status codes greater than 1000), either the operation code extender 'E' or an error indicator ER can be specified, but not both. For more information on error handling, see File Exception/Errors.

Remember the following when using the UPDATE operation:
  • When name is a record format name, the current values in the program for the fields in the record definition are used to modify the record.
  • If some but not all fields in a record are to be updated, either use the output specifications without an UPDATE operation or use %FIELDS to identify which fields to update.
  • Before UPDATE is issued to a file or record, a valid input operation with lock (READ, READC, READE, READP, READPE, CHAIN, or primary/secondary file) must be issued to the same file or record. If the read operation returns with an error condition or if it was read without locking, the record is not locked and UPDATE cannot be issued. The record must be read again with the default of a blank operation extender to specify a lock request.
  • Consecutive UPDATE operations to the same file or record are not valid. Intervening successful read operations must be issued to position to and lock the record to be updated.
  • Beware of using the UPDATE operation on primary or secondary files during total calculations. At this stage in the RPG IV cycle, the fields from the current record (the record that is about to be processed) have not yet been moved to the processing area. Therefore, the UPDATE operation updates the current record with the fields from the preceding record. Also, when the fields from the current record are moved to the processing area, they are the fields that were updated from the preceding record.
  • For multiple device files, specify a subfile record format as the name operand. The operation is processed for the program device identified in the fieldname specified using the DEVID keyword in the file specification. If the program device is not specified, the device used in the last successful input operation is used. This device must be the same one you specified for the input operation that must precede the UPDATE operation. You must not process input or output operations to other devices in between the input and UPDATE operations. If you do, your UPDATE operation will fail.
  • For a display file which has multiple subfile record formats, you must not process read-for-update operations to one subfile record in between the input and UPDATE operations to another subfile in the same display file. If you do, the UPDATE operation will fail.
  • An UPDATE operation is valid to a subfile record format as long as at least one successful input operation (READC, CHAIN) has occurred to that format name without an intervening input operation to a different format name. The record updated will be the record retrieved on the last successful input operation. This means that if you read a record successfully, then read unsuccessfully to the same format, an update will succeed, but will update the first record. This is different from the behavior of DISK files.

    To avoid updating the wrong record, check the resulting indicator or record-identifying indicator to ensure that a successful input operation has occurred before doing the update operation.

See Database Null Value Support for information on updating records with null-capable fields containing null values.

For more information, see File Operations.