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-n
is the name of a record declared in theFILE
Section.identifier-n
is a data element, literal, or data returned from a function call.statement-n
is an imperative statement.
General Rules:
- The
REWRITE
statement can only be issued on records in a file that isOPEN I-O
. - The
REWRITE
statement 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 aREWRITE
statement is the record most recently retrieved by a successfulREAD
statement. The effect of theREWRITE
statement is to replace the contents of the record with the new record. - For files declared with
ACCESS IS RANDOM
orACCESS IS DYNAMIC
, the target of aREWRITE
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. - The
REWRITE
statement does not allow records to altered in length. - The
FROM
phrase causes aMOVE
fromidentifier-1
to the record area ofrecord-1
prior to the execution of theREWRITE
statement. - The
WITH [NO] LOCK
phrase is optional. - The
INVALID KEY
/NOT INVALID KEY
phrase requires that the file be ofORGANIZATION RELATIVE
orORGANIZATION INDEXED
. - 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 theREWRITE
to fail. If anINVALID KEY
phrase is used in theREWRITE
statement, then the followingstatement-list
is executed. If noINVALID KEY
condition is used, then the condition may be detected in theDECLARATIVES
. If there are noDECLARATIVES
, then the program aborts.
- The
NOT INVALID KEY
condition exists if theINVALID KEY
condition is not triggered. If aNOT INVALID KEY
phrase is used in theREWRITE
statement, then the followingstatement-list
is executed.- The
REWRITE
statement updates theFILE STATUS
variable.
- 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.