File REWRITE Example


Scenario - Rewriting a record by increasing the salary by 5000 for E0006 employee in EMPFILE (KSDS).

Input file (KSDS) -

Browse           MATESY.EMPLOYEE.DETAILS
Command ===>                            
                            Type KSDS   
Key
<===>----10---+----2----+----3----+----4
****  Top of data  ****                 
E0001EMPLOYEE1      MANAGER   0000200000
E0002EMPLOYEE2      TL        0000150000
E0003EMPLOYEE3      SE        0000050000
E0004EMPLOYEE4      SSE       0000040000
E0005EMPLOYEE5      SE        0000045000
E0006EMPLOYEE6      SE        0000040000
****  End of data  ****

Code -

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

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

       DATA DIVISION.
       FILE SECTION.
	  * EMPFILE structure definition
       FD EMPFILE  
           RECORD CONTAINS 80  CHARACTERS 
           BLOCK  CONTAINS 800 CHARACTERS 
           DATA RECORD     IS EMPFILE-RECORD.
	  * EMPFILE record structure definition
       01 EMPFILE-RECORD. 
          05 EMP-ID        PIC X(05).
          05 EMP-NAME      PIC X(15).
          05 EMP-DESG      PIC X(10). 
          05 EMP-SALARY    PIC 9(10). 
          05 FILLER        PIC X(40). 

       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 file for rewrite
           OPEN I-O EMPFILE.
      * Reading the record that need to update
           MOVE 'E0006'        TO EMP-ID.
           READ EMPFILE
                KEY IS EMP-ID
                    INVALID KEY DISPLAY "RECORD NOT FOUND"
                NOT INVALID KEY PERFORM 1000-REWRITE-REC
                                   THRU 1000-EXIT
           END-READ.
      * Closing file
           CLOSE EMPFILE.
           STOP RUN.

       1000-REWRITE-REC.
      * Increased salary by 5000
           COMPUTE EMP-SALARY = EMP-SALARY + 5000
      * Rewriting the record
           REWRITE EMPFILE-RECORD
                       INVALID KEY DISPLAY "RECORD NOT UPDATED"
                   NOT INVALID KEY DISPLAY "RECORD UPDATED" 
           END-REWRITE. 

       1000-EXIT.
            EXIT.

Run JCL -

//MATESYF JOB MSGLEVEL=(1,1),NOTIFY=&SYSUID
//*                                                 
//STEP01  EXEC PGM=FILERWRT
//STEPLIB  DD  DSN=MATESY.COBOL.LOADLIB,DISP=SHR
//INPUT01  DD  DSN=MATESY.EMPLOYEE.DETAILS,DISP=SHR 
//SYSOUT   DD  SYSOUT=*

Output -

RECORD UPDATED   

Output file (KSDS) -

Browse           MATESY.EMPLOYEE.DETAILS
Command ===>                            
                            Type KSDS   
Key
<===>----10---+----2----+----3----+----4
****  Top of data  ****                 
E0001EMPLOYEE1      MANAGER   0000200000
E0002EMPLOYEE2      TL        0000150000
E0003EMPLOYEE3      SE        0000050000
E0004EMPLOYEE4      SSE       0000040000
E0005EMPLOYEE5      SE        0000045000
E0006EMPLOYEE6      SE        0000045000
****  End of data  ****

Explaining Example -

In the above example:

  • KSDS file is used as an input and trying to rewrite a record by increasing the salary.
  • Here, random mode is used to read the record that should rewrite. If present, the record gets rewritten with new salary. Otherwise, "RECORD NOT UPDATED" displayed.