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:
record-nis the name of a record declared in theFILESection.identifier-nis a data element, literal, or data returned from a function call.statement-nis an imperative statement.
General Rules:
- The
REWRITEstatement can only be issued on records in a file that isOPEN I-O. - The
REWRITEstatement allows non-key fields to be altered and re-written to a file that isOPEN I-O. - For files declared with
ACCESS IS SEQUENTIAL, the only valid target of aREWRITEstatement is the record most recently retrieved by a successfulREADstatement. The effect of theREWRITEstatement is to replace the contents of the record with the new record. - For files declared with
ACCESS IS RANDOMorACCESS IS DYNAMIC, the target of aREWRITEstatement 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. - The
REWRITEstatement does not allow records to altered in length. - The
FROMphrase causes aMOVEfromidentifier-1to the record area ofrecord-1prior to the execution of theREWRITEstatement. - The
WITH [NO] LOCKphrase is optional. - The
INVALID KEY/NOT INVALID KEYphrase requires that the file be ofORGANIZATION RELATIVEorORGANIZATION INDEXED. - The
INVALID KEYcondition 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 KEYcondition causes theREWRITEto fail. If anINVALID KEYphrase is used in theREWRITEstatement, then the followingstatement-listis executed. If noINVALID KEYcondition is used, then the condition may be detected in theDECLARATIVES. If there are noDECLARATIVES, then the program aborts.
- The
NOT INVALID KEYcondition exists if theINVALID KEYcondition is not triggered. If aNOT INVALID KEYphrase is used in theREWRITEstatement, then the followingstatement-listis executed.- The
REWRITEstatement updates theFILE STATUSvariable.
- The
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.