Let's understand Mainframe
Home Tutorials Interview Q&A Quiz Mainframe Memes Contact us About us

Module 10: File processing


WRITE statement

  • It is used to insert logical record to an output or input/output file.
  • When WRITE statement is coded without FROM clause, record to be inserted should be moved to record buffer and then perform WRITE operation. Once WRITE operation completes, record is vanished from record buffer.
  • When WRITE is to be performed on file with sequential access mode, it should be opened in OUTPT or EXTEND mode
  • When WRITE is to be performed on file with random or dynamic access mode, it should be opened in OUTPUT or I-O mode.
  • Basic syntax [Format 1 for sequential files]

    WRITE record-name [FROM ws-data-item] [{BEFORE/AFTER} ADVANCING integer-1 {LINE/LINES}] [END-WRITE].

    Note:- BEFORE,AFTER clause is not valid for VSAM files
  • In above syntax,
    • record-name must be a data division FD entry. It must be noted that once write operation completes, record is no longer available in record-name
    • FROM ws-data-item is used to specify that data contained in ws-data-item should be written to file. This is equivalent of below statements:-

      MOVE ws-data-item TO record-name WRITE record-name

    • BEFORE ADVANCING specifies that record is to be written before line pointer is advanced by integer- times
    • AFTER ADVANCING specifies that record is to be written, after line pointer is advanced by integer-1 times
  • Basic syntax: [ Format 2 for indexed and relative files ]

    WRITE record-name  [FROM ws-data-item]       [INVALID KEY imperative-statement-1]       [NOT INVALID KEY imperative-statement-2] [END-WRITE]

  • In above syntax,
    • record-name must be a data division FD entry. It must be noted that once write operation completes, record is no longer available in record-name
    • FROM ws-data-item is used to specify that data contained in ws-data-item should be written to file. This is equivalent of below statements:-

      MOVE ws-data-item TO record-name WRITE record-name

    • INVALID KEY imperative-statement-1 : This becomes true when record is not found for specified key and thus causes execution of imperative-statement-1
    • NOT INVALID KEY imperative-statement-2: This is used to specify statements to be executed when WRITE operation succeeds

Exmpale of Sequential WRITE:-

INPUT FILE (PS DATASET):-

10000STEVE OKURA 20000AMIT KHATRI 30000KARISHMA KAIF

COBOL PROGRAM:-

000100 IDENTIFICATION DIVISION. 000200 PROGRAM-ID. WRITEPGM. 000300 ENVIRONMENT DIVISION. 000400 INPUT-OUTPUT SECTION. 000500 FILE-CONTROL. 000600 SELECT EMPLOYEE ASSIGN TO EMPL 000700 ORGANIZATION IS SEQUENTIAL 000800 ACCESS IS SEQUENTIAL. 000900 DATA DIVISION. 001000 FILE SECTION. 001100 FD EMPLOYEE. 001200 01 EMPLOYEE-REC. 001300 05 EMPL-ID PIC 9(05). 001400 05 EMPL-NAME PIC X(15). 001500 PROCEDURE DIVISION. 001600 MAIN-PARA. 001700 OPEN EXTEND EMPLOYEE 001800 001900 MOVE '40000' TO EMPL-ID 002000 MOVE 'ASHIK SHARMA' TO EMPL-NAME 002100 002200 WRITE EMPLOYEE-REC. 002300 CLOSE EMPLOYEE 002400 STOP RUN.

Mainframe Job Step to run above program:-

//STEP01 EXEC PGM=WRITEPGM //STEPLIB DD DSN=USER.TEST.LOADLIB,DISP=SHR //EMPL DD DSN=MAINFRAM.FILE.MAIN,DISP=SHR //SYSOUT DD SYSOUT=*

PS Dataset After Job run:-

10000STEVE OKURA 20000AMIT KHATRI 30000KARISHMA KAIF 40000ASHIK SHARMA






© copyright mainframebug.com
Privacy Policy