REWRITE Statement COBOL

The REWRITE statement logically replaces a record existing in a mass storage file.

REWRITE Statement

record-name-1 and identifier-1 must not refer to the same storage area.

record-name-1 is the name of a logical record in the File Section of the Data Division and may be qualified.

The file associated with record-name-1 must be a mass storage file and must be open in the I-O mode at the time of execution of this statement.

The INVALID KEY and the NOT INVALID KEY phrases must not be specified for a REWRITE statement which references a sequential file or a relative file in sequential access mode.

The INVALID KEY phrase must be specified in the REWRITE statement for files in the random or dynamic access mode for which an appropriate USE procedure is not specified.

For indexed files the INVALID KEY phrase must be specified in the REWRITE statement for files for which an appropriate USE procedure is not specified.

For files in the sequential access mode, the last input-output statement executed for the associated file prior to the execution of the REWRITE statement must have been a successfully executed READ statement.The runtime system replaces the record that was accessed by that READ statement.

When an indexed file is described with the DUPLICATES phrase in the RECORD KEY clause, the REWRITE statement in the dynamic access mode is executed as if the file were in the sequential access mode and the REWRITE statement in the random access mode is not allowed.

The file position indicator is not affected by the execution of a REWRITE statement.

The execution of the REWRITE statement causes the value of the file status data item, if any, associated with the file to be updated.

The record to be replaced by the execution of the REWRITE statement must not be locked by another run unit.For a shared input-output file, the run unit executing the REWRITE statement should obtain a record lock by preceding the REWRITE statement with a READ statement that locks the record to be replaced.If the run unit does not already hold a lock on the record to be replaced, the runtime system will attempt to obtain the lock.If the lock cannot be obtained because another run unit holds a lock on the record, subsequent action of the program is as described for the READ statement when attempting to lock a record already locked by another run unit.If the lock cannot be obtained because this run unit holds a lock on the record through another COBOL file-name, the REWRITE statement is unsuccessful.See “File Locking ” and “Record Locking” for additional information on coordinating file updates in a shared file environment.

In single record locking modes, any record lock held by the run unit for the file associated with record-name-1 is released after execution of the REWRITE statement.

In multiple record locking modes, record locks are not released except for the record lock obtained by the runtime system when the record to be replaced was not locked by the run unit prior to execution of the REWRITE statement.

For a relative file accessed in a random or dynamic access mode, the runtime system replaces the record specified by the contents of the relative key data item of the file.If the file does not contain the record selected by that key value, the invalid key condition exists.

For an indexed file accessed in the sequential access mode, the record to be replaced is selected by the value of the prime record key.When the REWRITE statement is executed, the value of the prime record key of the record to be replaced must be equal t the value of the prime record key of the last record read from the file.When the DUPLICATES phrase is specified in the RECORD KEY clause of the file control entry for the file, the record to be replaced is the one accessed by the previously executed READ statement.

For an indexed file in the random or dynamic access mode, the record to be replaced is selected by the prime record key.

For an indexed file, execution of the REWRITE statement for a record that has an alternate record key occurs as follows:

  • When the value of a specific alternate record key is not changed, the order of retrieval when that key is the key of reference remains unchanged.
  • When the value of a specific alternate record key is changed, the subsequent order of retrieval of that record may be changed when that specific alternate record key is the key of reference.When duplicate key values are permitted, the record is logically positioned last within the set of duplicate records containing the same alternate record key value as the alternate record key value that was placed in the record.

For indexed files the invalid key condition exists under any of the following circumstances:

  • When the access mode of the file is sequential and the value of the prime record key of the record to be replaced is not equal to the value of the prime record key of the last record read from the file.
  • When the access mode of the file is dynamic or random and the value of the prime record key of the record to be replaced is not equal to the value of the prime record key of any record existing in the file.
  • When the value of an alternate record key of the record to be replaced, for which duplicates are not allowed, equals the value of the corresponding data item of a record already existing in the file.

When the invalid key condition is recognized for both relative and indexed files, the execution of the REWRITE statement is unsuccessful, the updating operation does not take place, the contents of the record area are unaffected, and the I-O status value of the file associated with record-name-1 is set to a value indicating the cause of the condition.

Transfer of control following the successful or unsuccessful execution of the REWRITE operation depends on the presence or absence of the optional INVALID KEY and NOT INVALID KEY phrases in the REWRITE statement.

See the discussions of relative organization input-output and indexed organization input-output, for additional information on the invalid key condition and the use of the INVALID KEY phrase.

For sequential files, if the number of character positions in the record referenced by record-name-1 is not equal to the number of character positions in the record being replaced, the execution of the REWRITE statement is unsuccessful, the updating operation does not take place, the contents of the record area are unaffected, and the I-O status value of the file associated with record-name-1 is set toa value indicating the cause of the condition.

For relative and indexed files, the number of character positions in the record referenced by record-name-1 need not be the same as the number of character positions in the record being replaced.However, if it is larger than the largest or smaller than the smallest number of character positions allowed by the RECORD IS VARYING clause associated with the file, the execution of the REWRITE statement is unsuccessful, the updating operation does not take place, the contents of the record area are unaffected, and the I-O status value of the file associated with record-name-1 is set to a value indicating the cause of the condition.

FROM Phrase

FROM Phrase

The execution of a REWRITE statement with the FROM phrase is equivalent to the execution of a move from identifier-1 or literal-1 to record-name-1 followed by the execution of the same REWRITE statement without the FROM phrase.The contents of the record area prior to the execution of the implicit MOVE statement have no effect on the execution of the REWRITE statement.

REWRITE Statement Examples



Face Book Twitter Google Plus Instagram Youtube Linkedin Myspace Pinterest Soundcloud Wikipedia

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

COBOL Topics