Write command is used to add new records to a file. The parameters used in Write command are same as we had described before. Data is picked from the data area mentioned in the FROM clause. Following is the syntax for Write command −
EXEC CICS WRITE FILE(name) FROM(data-area) RIDFLD(data-area) LENGTH(data-value) KEYLENGTH(data-value) END-EXEC.
Following is the example to write a record in 'FL001' file where Student-id is the primary key and 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. MOVE '101Mohtahim M TutorialsPoint' TO WS-STD-REC. EXEC CICS WRITE FILE ('FL001') FROM (WS-STD-REC) LENGTH (WS-STD-REC-LEN) RIDFLD (WS-STD-REC-KEY) KEYLENGTH (WS-STD-KEY-LEN) END-EXEC.
The following table shows the list of exceptions that arise during a WRITE statement −
|Sr.No||Exception & Description|
File is not open.
File entry is not made in FCT.
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.
If more than 1 record satisfy the condition on the alternate key.
There is not enough space in the dataset.