The REWRITE statement logically replaces an existing record in a direct-access file. When the REWRITE statement is executed, the associated direct-access file must be open in I-O mode.
The REWRITE statement is not supported for line-sequential files.
MOVE identifier-1 TO record-name-1. REWRITE record-name-1
The MOVE is performed according to the rules for the MOVE statement without the CORRESPONDING phrase.
identifier-1 must be a valid sending item with record-name-1 as the receiving item in accordance with the rules of the MOVE statement.
identifier-1 and record-name-1 must not refer to the same storage area.
After the REWRITE statement is executed, the information is still available in identifier-1 (INTO and FROM phrases under “Common processing facilities”).
(See Invalid key condition under “Common processing facilities”.)
An INVALID KEY condition exists when:
This explicit scope terminator serves to delimit the scope of the REWRITE statement. END-REWRITE permits a conditional REWRITE statement to be nested in another conditional statement. END-REWRITE can also be used with an imperative REWRITE statement.
For more information, see Delimited scope statements.
After successful execution of a REWRITE statement, the logical record is no longer available in record-name-1 unless the associated file is named in a SAME RECORD AREA clause (in which case, the record is also available as a record of the other files named in the SAME RECORD AREA clause).
The file position indicator is not affected by execution of the REWRITE statement.
If the FILE STATUS clause is specified in the file-control entry, the associated file status key is updated when the REWRITE statement is executed.
For files in the sequential access mode, the last prior input/output statement executed for this file must be a successfully executed READ statement. When the REWRITE statement is executed, the record retrieved by that READ statement is logically replaced.
The number of character positions in record-name-1 must equal the number of character positions in the record being replaced.
The INVALID KEY phrase must not be specified for a file with sequential organization. An EXCEPTION/ERROR procedure can be specified.
The number of character positions in record-name-1 can be different from the number of character positions in the record being replaced.
When the access mode is sequential, the record to be replaced is specified by the value contained in the prime RECORD KEY. When the REWRITE statement is executed, this value must equal the value of the prime record key data item in the last record read from this file.
Both the INVALID KEY phrase and an applicable EXCEPTION/ERROR procedure can be omitted.
When the access mode is random or dynamic, the record to be replaced is specified by the value contained in the prime RECORD KEY.
Values of ALTERNATE RECORD KEY data items in the rewritten record can differ from those in the record being replaced. The system ensures that later access to the record can be based upon any of the record keys.
If an invalid key condition exists, the execution of the REWRITE statement is unsuccessful, the updating operation does not take place, and the data in record-name-1 is unaffected. (See Invalid key condition under “Common processing facilities”.)
The number of character positions in record-name-1 can be different from the number of character positions in the record being replaced.
For relative files in sequential access mode, the INVALID KEY phrase must not be specified. An EXCEPTION/ERROR procedure can be specified.
For relative files in random or dynamic access mode, the INVALID KEY phrase or an applicable EXCEPTION/ERROR procedure can be specified. Both can be omitted.
When the access mode is random or dynamic, the record to be replaced is specified in the RELATIVE KEY data item. If the file does not contain the record specified, an invalid key condition exists, and, if specified, the INVALID KEY imperative-statement is executed. (See Invalid key condition under “Common processing facilities”.) The updating operation does not take place, and the data in record-name is unaffected.