REWRITE Statement

The REWRITE statement logically replaces a record in a file.

General Format

REWRITE record [ FROM source-field ]

    [ INVALID KEY statement-1 ]

    [ NOT INVALID KEY statement-2 ]

    [ END-REWRITE ]

Syntax Rules

  1. record must be the name of a logical record in the Data Division File Section. The associated file may not be a sort file.
  2. source-field is a data item or literal.
  3. statement-1 and statement-2 are imperative statements.
  4. The INVALID KEY and NOT INVALID KEY phrases may not be specified for sequential files or relative files with sequential access.
  5. record and source-field may not share any storage area.

General Rules

  1. The file associated with record must be a mass storage file and must be open in the I-O mode.
  2. For files with sequential access mode, the preceding I/O statement executed for the file must have been a successful READ statement. The REWRITE statement replaces the last record read by the contents of record. If the file is an indexed file, the primary key must not have been changed since the last READ.
  3. For random or dynamic access mode files, the REWRITE statement replaces the record specified by the file's key.

    For relative files, this is the record specified by its RELATIVE KEY data item. For indexed files, the record identified by the primary key is replaced.

  4. For an indexed file with alternate keys, the order in which duplicated keys are subsequently returned is affected as follows:
    1. If the value of an alternate key has not changed, its order of retrieval is unchanged.
    2. If the value is changed, and the new value is a duplicated value, the record's logical position is unpredictable within the set of records with that value.
  5. The REWRITE statement does not affect the current file position.
  6. The following occurrences cause the invalid-key condition:
    1. The access mode is sequential and an indexed file's primary key is not identical to the value returned from the preceding READ statement.
    2. The record being replaced does not exist in the file.
    3. The value of an alternate key that does not allow duplicates equals that of another record already in the file.

    The invalid-key condition causes the REWRITE to fail and does not update the file.

  7. If the invalid-key condition occurs, and there is an INVALID KEY phrase, statement-1 executes. If there is no INVALID KEY phrase, but there is an appropriate USE AFTER EXCEPTION procedure, that procedure executes. Otherwise, an invalid-key condition causes a message to be printed and the program halts.
  8. If the NOT INVALID KEY phrase is specified, statement-2 executes if the REWRITE statement is successful.
  9. For a sequential file, the size of the record must be the same as the one it is replacing. The size of the record written is determined by the size of record.
  10. The REWRITE statement updates the value of the FILE STATUS data item for the file.
  11. If the FROM phrase is specified, it is identical to first moving the value of source-field to record using the rules of the MOVE statement and then performing the REWRITE as if there were no FROM phrase.