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

Module 10: File processing


READ statement

  • READ is used to read a record from file
  • At execution of a READ statement, only one record can be read. It also increments pointer to support successive reads.
  • READ operation can be performed only when file is opened in INPUT or I-O mode.
  • Basic syntax (Format 1 for sequential retrieval)

    READ file-name [NEXT RECORD]      [INTO ws-data-record]      [AT END imperative-statement-1]      [NOT AT END imperative-statement-2] [END-READ].

  • Where,
    • file-name is logical file name
    • NEXT RECORD is optional to specify. It reads the next record in logical sequence of records. It is ignored when access mode is sequential
    • You must code NEXT RECORD phrase to retrieve records sequentially from files in dynamic access mode.
    • INTO ws-data-record : Indicates the record read from file should be placed in working storage item ‘ws-data-record’. It is optional
    • AT END imperative-statement-1 : This is used to capture the file end. When read operation encounters file end, this statement becomes true and imperative-statement-1 is executed; skipped otherwise
    • NOT AT END imperative-statement-2 :  imperative-statement-2 is executed when READ operation succeeds.
  • Example of Sequential READ:-
  • INPUT FILE (PS DATASET):-

    10000STEVE OKURA 20000AMIT KHATRI 30000KARISHMA KAIF

    COBOL PROGRAM:-

    000100 IDENTIFICATION DIVISION. 000200 PROGRAM-ID. SEQREAD. 000300 ENVIRONMENT DIVISION. 000400 INPUT-OUTPUT SECTION. 000500 FILE-CONTROL. 000600 SELECT EMPLOYEE ASSIGN TO EMPL 000700 ORGANIZATION IS SEQUENTIAL 000800 ACCESS IS SEQUENTIAL. 001000 DATA DIVISION. 001100 FILE SECTION. 001200 FD EMPLOYEE. 001300 01 EMPLOYEE-REC. 001400 05 EMPL-ID PIC 9(05). 001500 05 EMPL-NAME PIC X(15). 001600 WORKING-STORAGE SECTION. 001700 01 WS-EMPL. 001800 05 WS-EMPL-ID PIC 9(05). 001900 05 WS-EMPL-NAME PIC X(15). 002000 01 WS-EOF-FLAG PIC A(01). 002100 PROCEDURE DIVISION. 002200 MAIN-PARA. 002300 OPEN INPUT EMPLOYEE. 002400 PERFORM UNTIL WS-EOF-FLAG = 'Y' 002500 READ EMPLOYEE INTO WS-EMPL 002600 AT END 002700 MOVE 'Y' TO WS-EOF-FLAG 002800 NOT AT END 002900 DISPLAY 'REC : ' WS-EMPL 003000 END-READ 003100 END-PERFORM. 003200 CLOSE EMPLOYEE 003300 STOP RUN.

    Mainframe Job Step to run above program:-

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

    SYSOUT After Job run:-

    REC : 10000STEVE OKURA REC : 20000AMIT KHATRI REC : 30000KARISHMA KAIF

  • Basic syntax [Format 2 for random retrieval]

    READ file-name-1 RECORD [INTO ws-data-record]         [KEY IS data-item-name]         [INVALID KEY imperative-statement-3]         [NOT INVALID KEY imperative-statement-4] [END-READ].

  • Where,
    • file-name-1 is logical file name
    • INTO ws-data-record indicates the record read from file should be placed in working storage data item ‘ws-data-record’. It is optional
    • KEY IS phrase is coded to specify key to describe which record to be pulled. It is coded only for Indexed files.
    • INVALID KEY imperative-statement-3 :- This becomes true when record is not found for specified key and thus causes execution of imperative-statement-3
    • NOT INVALID KEY imperative-statement-4 : This is used to specify statements to be executed when READ operation succeeds.
  • Example of Random READ:-
  • INPUT FILE (KSDS DATASET):-

    10000STEVE OKURA 20000AMIT KHATRI 30000KARISHMA KAIF

    COBOL PROGRAM:-

    000100 IDENTIFICATION DIVISION. 000200 PROGRAM-ID. RNDMREAD. 000300 ENVIRONMENT DIVISION. 000400 INPUT-OUTPUT SECTION. 000500 FILE-CONTROL. 000600 SELECT EMPLOYEE ASSIGN TO EMPL 000700 ORGANIZATION IS INDEXED 000800 ACCESS IS RANDOM 000900 RECORD KEY IS EMPL-ID. 001000 DATA DIVISION. 001100 FILE SECTION. 001200 FD EMPLOYEE. 001300 01 EMPLOYEE-REC. 001400 05 EMPL-ID PIC 9(05). 001500 05 EMPL-NAME PIC X(15). 001600 WORKING-STORAGE SECTION. 001700 01 WS-EMPL. 001800 05 WS-EMPL-ID PIC 9(05). 001900 05 WS-EMPL-NAME PIC X(15). 002000 PROCEDURE DIVISION. 002100 MAIN-PARA. 002200 OPEN INPUT EMPLOYEE 002300 MOVE '20000' TO EMPL-ID 002400 READ EMPLOYEE INTO WS-EMPL 002500 KEY IS EMPL-ID 002600 INVALID KEY 002700 DISPLAY 'RECEORD KEY IS INVALID' 002800 NOT INVALID KEY 002900 DISPLAY 'REC : ' WS-EMPL 003000 END-READ 003100 CLOSE EMPLOYEE 003200 STOP RUN.

    Mainframe Job Step to run above program:-

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

    SYSOUT After Job run:-

    REC : 20000AMIT KHATRI






© copyright mainframebug.com
Privacy Policy