The REWRITE Statement
Syntax
Format 1
REWRITE record-name-1 [ FROM { identifier-1
literal-1 }]
Format 2
REWRITE record-name-1 [ FROM { identifier-1
literal-1 }]
[INVALID KEY imperative-statement-1]
[NOT INVALID KEY imperative-statement-2]
[END-REWRITE]
Description
The REWRITE statement is used to replace records in a file. The record is replaced within the file associated with record-name-1. If the FROM clause is included, the FROM item is copied into record-name-1 before the data record is rewritten. If the record was locked during the READ, the REWRITE will unlock the record automatically. Each of the two formats works slightly differently, and is described below in its correspondingly numbered item.
1. The first format performs a sequential rewrite of the file. The last record read is replaced by the contents of record-name-1.
2. The second format performs a random rewrite of the file. The last record read, will be rewritten. If the record key has been changed, no record is replaced and the code between INVALID KEY and NOT INVALID KEY, if any, will be executed. If the rewrite is successful, the code between NOT INVALID KEY and END-REWRITE, if any, will be executed.
Tips
1. If there is a file status associated with the file, it will be set after the REWRITE is executed to indicate the result of the REWRITE. A value of "00" means successful and "23" means the record key was modified prior to the execution of the REWRITE.
2. Any file mentioned must be the subject of a SELECT statement and a file description (FD). Files must be open for input/output and read prior to executing the REWRITE statement.
3. When used in a program with a SORT statement, the sort file itself can never be rewritten.
4. Be sure to READ the record you wish to update and change the desired fields before executing the REWRITE statement.
Syntax
Format 1
REWRITE record-name-1 [ FROM { identifier-1
literal-1 }]
Format 2
REWRITE record-name-1 [ FROM { identifier-1
literal-1 }]
[INVALID KEY imperative-statement-1]
[NOT INVALID KEY imperative-statement-2]
[END-REWRITE]
Description
The REWRITE statement is used to replace records in a file. The record is replaced within the file associated with record-name-1. If the FROM clause is included, the FROM item is copied into record-name-1 before the data record is rewritten. If the record was locked during the READ, the REWRITE will unlock the record automatically. Each of the two formats works slightly differently, and is described below in its correspondingly numbered item.
1. The first format performs a sequential rewrite of the file. The last record read is replaced by the contents of record-name-1.
2. The second format performs a random rewrite of the file. The last record read, will be rewritten. If the record key has been changed, no record is replaced and the code between INVALID KEY and NOT INVALID KEY, if any, will be executed. If the rewrite is successful, the code between NOT INVALID KEY and END-REWRITE, if any, will be executed.
Tips
1. If there is a file status associated with the file, it will be set after the REWRITE is executed to indicate the result of the REWRITE. A value of "00" means successful and "23" means the record key was modified prior to the execution of the REWRITE.
2. Any file mentioned must be the subject of a SELECT statement and a file description (FD). Files must be open for input/output and read prior to executing the REWRITE statement.
3. When used in a program with a SORT statement, the sort file itself can never be rewritten.
4. Be sure to READ the record you wish to update and change the desired fields before executing the REWRITE statement.