Reading Fixed Length Indexed File Dynamically Example


Scenario - Reading fixed length KSDS file dynamically from employee id = E0004.

Input file -

Browse           MATEPK.EMPL.KSDS
Command ===>
                            Type KSDS     RBA
Key                                          Col 1
<===>----10---+----2----+----3----+----4----+--
****  Top of data  ****
E0001EMPLOYEE1     DIR       LOC1 0000100000
E0002EMPLOYEE2     MGR  DEPT1LOC1 0000080000
E0003EMPLOYEE3     MGR  DEPT2LOC2 0000075000
E0004EMPLOYEE4     TL   DEPT1LOC1 0000050000
E0005EMPLOYEE5     SSE  DEPT1LOC1 0000045000
E0006EMPLOYEE6     SE   DEPT1LOC1 0000034000
E0007EMPLOYEE7     SSE  DEPT2LOC2 0000046000
****  End of data  ****

Code -

----+----1----+----2----+----3----+----4----+----5----+
       IDENTIFICATION DIVISION.
       PROGRAM-ID. INDFLRDD.
       AUTHOR. MTH.

       ENVIRONMENT DIVISION.
       INPUT-OUTPUT SECTION.
       FILE-CONTROL.
      * EMPFILE Definition
           SELECT EMPFILE ASSIGN TO INPUT01
           ORGANIZATION IS INDEXED 
           ACCESS MODE  IS DYNAMIC
           RECORD KEY   IS EMP-ID
           FILE STATUS  IS WS-FS1.

       DATA DIVISION.
       FILE SECTION.
      * EMPFILE structure definition       
       FD EMPFILE
           RECORD CONTAINS 47  CHARACTERS
           BLOCK  CONTAINS 470 CHARACTERS
           DATA RECORD     IS EMPFILE-RECORD.
      * EMPFILE record structure definition 
       01 EMPFILE-RECORD.
          05 EMP-ID        PIC X(05).
          05 EMP-NAME      PIC X(14).
          05 EMP-DESG      PIC X(05).
          05 EMP-DEPT      PIC X(05).
          05 EMP-LOC       PIC X(05).
          05 EMP-SALARY    PIC 9(10).
          05 FILLER        PIC X(03).

       WORKING-STORAGE SECTION.
       01 WS-VAR.
          05 WS-FS1        PIC 9(02).
          05 WS-EOF-SW     PIC X(01).
             88 WS-EOF         VALUE 'Y'.
             88 WS-NOT-EOF     VALUE 'N'.

       PROCEDURE DIVISION.
      * Opening EMPFILE for reading
           OPEN INPUT EMPFILE.

      * Setting the browsing point using the input EMP-ID 
           ACCEPT EMP-ID.
           START EMPFILE
             KEY IS EQUAL EMP-ID
                    INVALID KEY DISPLAY "Record not found"
                NOT INVALID KEY PERFORM 1000-READ-RECORDS 
                                   THRU 1000-EXIT
           END-START.

      * Closing EMPFILE
           CLOSE EMPFILE.

           STOP RUN.
       1000-READ-RECORDS.

      * Loop for reading all records from EMPFILE
           SET  WS-NOT-EOF      TO  TRUE.
           PERFORM UNTIL WS-EOF
                READ EMPFILE NEXT RECORD
                         AT END SET WS-EOF TO TRUE    
                     NOT AT END DISPLAY EMPFILE-RECORD
                END-READ
           END-PERFORM.

       1000-EXIT.
           EXIT.

Run JCL -

//MATEPKF  JOB MSGLEVEL=(1,1),NOTIFY=&SYSUID     
//*
//STEP01  EXEC PGM=INDFLRDD
//STEPLIB  DD  DSN=MATEPK.COBOL.LOADLIB,DISP=SHR 
//INPUT01  DD  DSN=MATEPK.EMPL.KSDS,DISP=SHR
//SYSOUT   DD  SYSOUT=*
//SYSIN    DD  *
E0004
/*

Output -

E0004EMPLOYEE4     TL   DEPT1LOC1 0000050000
E0005EMPLOYEE5     SSE  DEPT1LOC1 0000045000
E0006EMPLOYEE6     SE   DEPT1LOC1 0000034000
E0007EMPLOYEE7     SSE  DEPT2LOC2 0000046000

Explaining Example -

In the above example:

  • Fixed length KSDS (MATEPK.EMPL.KSDS) file is used as an input file.
  • File organization is INDEXED and accessing mode should be DYNAMIC.
  • File reading starts from the record with the employee ID "E0004" and continue till the end of the file.