Rational Developer for System z
Enterprise COBOL for z/OS, Version 4.1, Language Reference


REWRITE statement

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.

Read syntax diagramSkip visual syntax diagram
Format: REWRITE statement

>>-REWRITE--record-name-1--+--------------------+--------------->
                           '-FROM--identifier-1-'   

>--+------------------------------------------+----------------->
   '-INVALID--+-----+--imperative-statement-1-'   
              '-KEY-'                             

>--+----------------------------------------------+------------->
   '-NOT INVALID--+-----+--imperative-statement-2-'   
                  '-KEY-'                             

>--+-------------+---------------------------------------------><
   '-END-REWRITE-'   

record-name-1
Must be the name of a logical record in a data division FD entry. The record-name can be qualified.
FROM phrase
The result of the execution of the REWRITE statement with the FROM identifier-1 phrase is equivalent to the execution of the following statements in the order specified.
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
identifier-1 can reference one of the following:
  • A record description for another previously opened file
  • An alphanumeric or national function
  • A data item defined in the working-storage section, the local-storage section, or the linkage section

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").

INVALID KEY phrases

An INVALID KEY condition exists when:

  • The access mode is sequential, and the value contained in the prime RECORD KEY of the record to be replaced does not equal the value of the prime RECORD KEY data item of the last-retrieved record from the file
  • The value contained in the prime RECORD KEY does not equal that of any record in the file
  • The value of an ALTERNATE RECORD KEY data item for which DUPLICATES is not specified is equal to that of a record already in the file

For details of invalid key processing, see Invalid key condition.

END-REWRITE phrase

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.

Reusing a logical record

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.

Sequential files

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.

Indexed files

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".)

Relative files

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.


Terms of use | Feedback

This information center is powered by Eclipse technology. (http://www.eclipse.org)