FILE WRITE Statement
FILE WRITE Statement
- A WRITE statement writes the records in the file.
- At a time, only one record is written to the file.
Points to note -
- The file should open in OUTPUT (for sequential, indexed, and relative files when the files are empty), I-O(for indexed and relative files when the files are not empty), or EXTEND (for sequential files when the file is not empty) mode to write a record.
- It is used for all types of (sequential, indexed, and relative) files.
- A simple READ statement is used when the ACCESS MODE is SEQUENTIAL, RANDOM, or DYNAMIC.
Syntax -
For Sequential files -
WRITE record-name
[FROM ws-record-name]
[BEFORE ADVANCING ws-variable|num LINES]
[AFTER ADVANCING ws-variable|num LINES]
[END-WRITE].
For Index and Relative files -
WRITE record-name
[FROM ws-record-name]
[INVALID KEY statements-set1]
[NOT INVALID KEY statements-set2]
[END-WRITE].
Note! All statements coded in [ ] are optional.
Parameters -
- record-name - Specifies the record data item where the record will be written.
- FROM ws-record-name - Optional. It indicates the record source that will be written to the file. If this clause is ignored, the data will be taken from the record-name.
- BEFORE ADVANCING - Specifies the number of lines to forward before writing a record.
- AFTER ADVANCING - Specifies the number of lines to forward after writing a record.
- END-WRITE - Optional. Specifies the end of the WRITE statement. END-WRITE is not required when a WRITE ends with a period.
Error Handling -
- INVALID KEY - This phrase specifies the action to be taken if there's an error during the write operation, like attempting to add a record with a duplicate key in an indexed file. The statements following INVALID KEY are executed in such cases. This is applicable to indexed or relative files.
- NOT INVALID KEY - This is executed if the WRITE operation is completed without errors. This is applicable to indexed or relative files.
Note! If the FILE-STATUS clause is coded, the associated file status is updated when the START statement is executed.
Practical Example -
Scenario - Below example describes how to write a new record into a new PS file.
Code -
----+----1----+----2----+----3----+----4----+
...
PROCEDURE DIVISION.
* Opening the file for writing
OPEN OUTPUT EMPFILE.
INITIALIZE EMPFILE-RECORD
* Receiving all the information of the record
ACCEPT EMP-ID.
ACCEPT EMP-NAME.
ACCEPT EMP-DESG.
ACCEPT EMP-SALARY.
* Writing record into file and validation
WRITE EMPFILE-RECORD.
IF WS-FS1 EQUAL ZERO
DISPLAY "RECORD INSERTED"
ELSE
DISPLAY "RECORD INSERTION FAILED"
END-IF.
* Closing file
CLOSE EMPFILE.
...
Run JCL -
//MATESYF JOB MSGLEVEL=(1,1),NOTIFY=&SYSUID //* //STEP01 EXEC PGM=FILEWRIT //STEPLIB DD DSN=MATESY.COBOL.LOADLIB,DISP=SHR //INPUT01 DD DSN=MATESY.EMPLOYEE.INPFILE1, // DISP=(NEW,CATLG,DELETE), // SPACE=(TRK,(1,1),RLSE), // UNIT=SYSDA, // DCB=(DSORG=PS,RECFM=FB,LRECL=80,BLKSIZE=800) //SYSOUT DD SYSOUT=* //SYSIN DD * E0006 EMPLOYEE6 SE 0000040000 /*
Output -
RECORD INSERTED
MATESY.EMPLOYEE.INPFILE1 -
E0006EMPLOYEE6 SE 0000040000