The REWRITE statement logically replaces a record in a file.
REWRITE record [ FROM source-field ]
[ INVALID KEY statement-1 ]
[ NOT INVALID KEY statement-2 ]
[ END-REWRITE ]
- record must be the name of a logical record in the Data Division File Section. The associated file may not be a sort file.
- source-field is a data item or literal.
- statement-1 and
statement-2 are imperative statements.
- The INVALID KEY and NOT INVALID KEY phrases may not be specified for sequential files or relative files with sequential access.
- record and
source-field may not share any storage area.
- The file associated with
record must be a mass storage file and must be open in the I-O mode.
- 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.
- 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.
- For an indexed file with alternate keys, the order in which duplicated keys are subsequently returned is affected as follows:
- If the value of an alternate key has not changed, its order of retrieval is unchanged.
- 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.
- The REWRITE statement does not affect the current file position.
- The following occurrences cause the
- The access mode is sequential and an indexed file's primary key is not identical to the value returned from the preceding
- The record being replaced does not exist in the file.
- The value of an alternate key that does not allow duplicates equals that of another record already in the file.
invalid-key condition causes the REWRITE to fail and does not update the file.
- 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.
- If the NOT INVALID KEY phrase is specified,
statement-2 executes if the REWRITE statement is successful.
- 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.
- The REWRITE statement updates the value of the FILE STATUS data item for the file.
- 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.