REWRITE command is used to modify a record that is already present in a file. Prior to this command, the record must be read with a READ UPDATE command. The parameters are same as described before. The syntax for the Rewrite command is as follows −
EXEC CICS REWRITE FILE (name) FROM (data-area) LENGTH (data-value) END-EXEC.
The following example shows how to write a record in 'FL001' file where Studentid is the primary key. A new record with 101 student id will be written in the file −
IDENTIFICATION DIVISION. PROGRAM-ID. HELLO. DATA DIVISION. WORKING-STORAGE SECTION. 01 WS-STD-REC-LEN PIC S9(4) COMP. 01 WS-STD-KEY-LEN PIC S9(4) COMP. 01 WS-STD-REC-KEY PIC 9(3). 01 WS-STD-REC PIC X(70). PROCEDURE DIVISION. MOVE +70 TO WS-STD-REC-LEN. MOVE ‘101’ TO WS-STD-REC-KEY. MOVE 3 TO WS-STD-KEY-LEN. EXEC CICS READ FILE ('FL001') INTO (WS-STD-REC) LENGTH (WS-STD-REC-LEN) RIDFLD (WS-STD-REC-KEY) KEYLENGTH (WS-STD-KEY-LEN) UPDATE END-EXEC. MOVE '100Mohtahim M TutorialsPnt' TO WS-STD-REC. EXEC CICS REWRITE FILE ('FL001') FROM (WS-STD-REC) LENGTH (WS-STD-REC-LEN) END-EXEC.
The following table lists the exceptions that arise during a REWRITE statement −
|Sr.No||Exception & Description|
File is not open.
Mismatch between the length specified in command and actual length of the record.
If the user does not have enough permissions to use the file.
Rewrite without prior READ with UPDATE.
There is not enough space in the dataset.