Skip to content

REWRITE Statement

The REWRITE statement modifies a record in an indexed file, posting modifications to non-key fields.

General Format:

       REWRITE record-1 [ FROM identifier-1 ] 
              [ WITH [NO] LOCK ] 
              [ INVALID KEY statement-1 ] 
              [ NOT INVALID KEY statement-2 ]
              [ END-REWRITE ]

Syntax:

  1. record-n is the name of a record declared in the FILE Section.
  2. identifier-n is a data element, literal, or data returned from a function call.
  3. statement-n is an imperative statement.

General Rules:

  1. The REWRITE statement can only be issued on records in a file that is OPEN I-O.
  2. The REWRITE statement allows non-key fields to be altered and re-written to a file that is OPEN I-O.
  3. For files declared with ACCESS IS SEQUENTIAL, the only valid target of a REWRITE statement is the record most recently retrieved by a successful READ statement. The effect of the REWRITE statement is to replace the contents of the record with the new record.
  4. For files declared with ACCESS IS RANDOM or ACCESS IS DYNAMIC, the target of a REWRITE statement is determined by the setting of the primary key in the case of indexed files, and the relative key in the case of relative files.
  5. The REWRITE statement does not allow records to altered in length.
  6. The FROM phrase causes a MOVE from identifier-1 to the record area of record-1 prior to the execution of the REWRITE statement.
  7. The WITH [NO] LOCK phrase is optional.
  8. The INVALID KEY/NOT INVALID KEY phrase requires that the file be of ORGANIZATION RELATIVE or ORGANIZATION INDEXED.
  9. The INVALID KEY condition is triggered by any of the following:
    • A change has been made to an indexed file’s primary key.
    • A changes has been made to an alternate key that does not allow duplicates, and the change has created a duplicate key condition.
    • The size of the record has changed
    • The INVALID KEY condition causes the REWRITE to fail. If an INVALID KEY phrase is used in the REWRITE statement, then the following statement-list is executed. If no INVALID KEY condition is used, then the condition may be detected in the DECLARATIVES. If there are no DECLARATIVES, then the program aborts.
  10. The NOT INVALID KEY condition exists if the INVALID KEY condition is not triggered. If a NOT INVALID KEY phrase is used in the REWRITE statement, then the following statement-list is executed.
    • The REWRITE statement updates the FILE STATUS variable.

Code Sample:

       IDENTIFICATION DIVISION.
       PROGRAM-ID. REWRITE-1. 
       ENVIRONMENT DIVISION. 
       INPUT-OUTPUT SECTION. 
       FILE-CONTROL.
              SELECT RESWORDS ASSIGN TO "RESWORDS" 
              ORGANIZATION IS INDEXED 
              ACCESS IS DYNAMIC 
              RECORD KEY IS RESERVED-WORD 
              FILE STATUS IS RESWORDS-STAT. 

       DATA DIVISION. 
       FILE SECTION. 
       FD RESWORDS. 
       01 RESWORDS-RECORD. 
              03 RESERVED-WORD PIC X(30). 
              03 COMMENT PIC X(20). 

       WORKING-STORAGE SECTION. 
       77 RESWORDS-STAT PIC XX. 
              88 END-OF-RESWORDS VALUE "10". 
       77 DUMMY PIC X. 

       PROCEDURE DIVISION. 
       MAIN. 
              OPEN OUTPUT RESWORDS. 
              MOVE "ACCEPT" TO RESERVED-WORD. 
              MOVE "HELLO WORLD" TO COMMENT. 
              WRITE RESWORDS-RECORD. 
              CLOSE RESWORDS. 

              OPEN I-O RESWORDS. 
              MOVE"ACCEPT"TO RESERVED-WORD. 
              READ RESWORDS. 
              MOVE "CHANGING COMMENT" TO COMMENT. 
              REWRITE RESWORDS-RECORD WITH LOCK 
                     INVALID KEY 
                            DISPLAY "REWRITE FAILED! " LINE 10 COL 10 
                            DISPLAY RESWORDS-STAT LINE 10 COL 26 
                     NOT INVALID KEY 
                            DISPLAY "REWRITE SUCCEEDS!" LINE 10 COL 10 
              END-REWRITE. 

              DISPLAY "REWRITE-1 FINISHED!" LINE 11 COL 10. 
              ACCEPT DUMMY LINE 11 COL 35. 

              CLOSE RESWORDS. 
              STOP RUN.
Back to top