REWRITE used to update the records that are already existed in the file. The REWRITE statement replaces an existing record with the new data in direct-access file.

The file must be opened in I-O mode for REWRITE statement. The REWRITE statement is not supported for line-sequential files.

Syntax -

REWRITE Syntax

record-name-1 -

Specifies the name of a logical record in a data division FD entry. The record-name can be qualified.

FROM phrase -

Specifies the data area that is not previously moved to record-name-1. FROM phrase avoids the explicit move of idetifier-1 to record-name-1. The FROM identifier-1 phrase is equivalent to the execution of the following statements -

MOVE identifier-1 to record-name-1. RELEASE record-name-1.

identifier-1 -

identifier-1 must reference one of the following -

  • An entry in the working-storage section or the linkage section.
  • A record description for another previously opened file.
  • An alphanumeric function.

identifier-1 and record-name-1 should not refer to the same storage area. After the REWRITE statement is executed, the information is still available in identifier-1.

INVALID KEY phrase -

An INVALID KEY condition exists when -

  • The access mode is sequential and the RECORD KEY value of the record to be replaced does not equal with the last-retrieved records RECORD KEY data item value from the file.
  • The RECORD KEY value does not equal with the any record in the file.
  • The ALTERNATE RECORD KEY data item value (DUPLICATES is not specified) is equal to the record that already in the file.

END-REWRITE phrase -

The END-REWRITE used to end the scope of the REWRITE statement. END-REWRITE is not required when REWRITE statement ended with period.

END-REWRITE can also be used with an imperative REWRITE statement. END-REWRITE permits conditional REWRITE to be nested in another conditional statement.

REWRITE on different file types -

If the FILE STATUS clause is specified, 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 record-name-1 length must equal to the length of 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 record-name-1 length can be different from the length of the record being replaced. The record to be replaced is identified by the RECORD KEY value. When the access mode is sequential -

  • When the REWRITE statement is executed, the value must equal to the record key data item value that was from the last record read.
  • Both the INVALID KEY phrase and an applicable EXCEPTION/ERROR procedure can be omitted.

When the access mode is random or dynamic -

  • Values of ALTERNATE RECORD KEY data items in the rewritten record can differ from those in the record being replaced.
  • If an INVALID KEY condition exists, the execution of the REWRITE statement is unsuccessful and the data in record-name-1 is unaffected.

Relative files -

The record-name-1 length can be different from the length of the record being replaced. When the access mode is sequential -

  • The INVALID KEY phrase must not be specified and an EXCEPTION/ERROR procedure can be specified.

When the access mode is sequential -

  • 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 identified by RELATIVE KEY data item value.
  • If the file does not contain the record specified, an INVALID KEY condition exists and the INVALID KEY imperative-statement is executed.

Practical Example -

Below example covers File REWRITE processing

Code:


 IDENTIFICATION DIVISION.                                         
 PROGRAM-ID. FILEIO.                                              
 ENVIRONMENT DIVISION.                                            
 INPUT-OUTPUT SECTION.                                            
 FILE-CONTROL.                                                    
     SELECT FILE1 ASSIGN TO DISK1.                                
     ORGANIZATION IS INDEXED.                                     
     ACCESS MODE IS RANDOM.                                       
     RECORD KEY  IS STD-NO.                                       
     FILE STATUS IS WS-FS.                                        
 DATA DIVISION.                                                   
 FILE SECTION.                                                    
 FD  FILE1.                                                       
     RECORD CONTAINS 80 CHARACTERS.                               
     BLOCK CONTAINS 800 CHARACTERS.                               
     RECORDING MODE IS F.                                         
     DATA RECORD IS STD-REC.                                      
 01 STD-REC.                                                      
     02 STD-NO          PIC 9(03).                                
     02 STD-NAME        PIC X(20).                                
     02 STD-GENDER      PIC X(07).                                
     02 FILLER          PIC X(50).                                
 WORKING-STORAGE SECTION.                                         
 77 WS-STD-NO           PIC 9(03).                                
 77 WS-STD-NAME         PIC X(20).                                
 77 WS-FS               PIC 9(02).                                
 01 WS-EOF-SW           PIC X(01) VALUE 'N'.                      
    88 EOF-SW           VALUE 'Y'.                                
    88 NOT-EOF-SW       VALUE 'N'.                                
 PROCEDURE DIVISION.                                              
     ACCEPT WS-STD-NO.                                            
     ACCEPT WS-STD-NAME.                                          
     MOVE WS-STD-NO    TO STD-NO.                                 
     DISPLAY 'INDEXED FILE READING...'.                           
     OPEN I-O FILE1.                                              
     READ FILE1                                                   
     AT END MOVE 'Y'     TO WS-EOF-SW.                            
     IF WS-FS = 00                                                
        MOVE WS-STD-NAME TO STD-NAME                              
        REWRITE STD-REC                                     
     ELSE                                                         
        DISPLAY ' RECORD NOT FOUND IN FILE...'                    
     END-IF.                                                      
     CLOSE FILE1.                                                 
     STOP RUN.