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

Module 10: File processing


REWRITE statement

  • REWRITE is used to update existing record. It overwrites the last record read.
  • REWRITE can be performed only after opening file in I-O mode.
  • Syntax:-

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

  • Where,
    • record-name must be a data division FD entry. It must be noted that once rewrite 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 REWRITE operation succeeds

REWRITE example

INPUT FILE (KSDS DATASET):-

10000STEVE OKURA 20000AMIT KHATRI 30000KARISHMA KAIF

COBOL PROGRAM:-

000100 IDENTIFICATION DIVISION. 000200 PROGRAM-ID. MFPROG2. 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 I-O 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 003200 MOVE 'ASHIK SHARMA' TO EMPL-NAME 003300 REWRITE EMPLOYEE-REC 003400 INVALID KEY 003500 DISPLAY 'RECEORD KEY IS INVALID' 003600 NOT INVALID KEY 003700 DISPLAY 'NEW REC : ' EMPLOYEE-REC 003800 END-REWRITE 003900 004000 CLOSE EMPLOYEE 004100 STOP RUN.

Mainframe Job Step to run above program:-

//STEP01 EXEC PGM=REWRITER //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 NEW REC : 20000ASHIK SHARMA






© copyright mainframebug.com
Privacy Policy