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

Module 12: SORT AND MERGE


MERGE

  • MERGE statement compares record from two or more sequenced files and combines them to write into one file in specific order
  • In merge process, three kind of files are used:-
    • input-file: Two or more input files that are to be merged
    • work-file:- Temporary work file, which will be used during merge process The SD(sort description) entry for file should be defined in FILE SECTION
    • output-file: File on which merged records are written
  • Basic syntax:-

    MERGE work-file ON {ASCENDING/DESCENDING}      KEY sd-rec-key-1 [,sd-rec-key-2]… USING input-file-1, input-file-2 [,input-file-3]… GIVING output-file.

  • Note :- For work-file, SD entry should be defined in FILE SECTION
  • Format of SD entry:-

    SD work-file    [RECORD CONTIANS integer CHARACTERS]    [DATA RECORD IS file-rec-1]

  • Important consideration about MERGE:-
    • All input files must be sequential files an must be sorted on merge keys
    • Each sd-rec-keys (shown in SORT syntax) identifies a field in the record of work-file, upon which the file will be sorted
    • When more than one sd-file-key is specified, the keys decrease in significance from left to right (leftmost key is most major key i.e. most significant rightmost is least significant)
    • It is possible to use both the words ASCENDING and DESCENDING in single SORT statement but for different keys. If all keys to be sorted in same order, then ASCENDING or DESCENDING needs to be mentioned only once before the most major key
    • The order of appearance of keys in SORT statement has no relevance in their position in the record.
    • All kind of files must have same records size and have same position of the merge keys

Example depicting use of MERGE

Suppose there are two different department-files containing employee information and we want to create one merged file containing all employees from both department. This can be achieved using MERGE. Sample program for same is given below:-

000100 IDENTIFICATION DIVISION. 000200 PROGRAM-ID. MERGEPGM. 000300 ENVIRONMENT DIVISION. 000400 INPUT-OUTPU SECTION. 000500 FILE-CONTROL. 000600 SELECT DEP-1 ASSIGN TO DEPT1. 000700 SELECT DEP-2 ASSIGN TO DEPT2. 000800 SELECT ALL-DEPT ASSIGN TO ALLDEPT. 000900 SELECT WORK-FILE ASSIGN TO WRKDD. 001000 DATA DIVISION. 001100 FILE-SECTION. 001200 FD DEPT-1. 001300 01 DEP1-1-REC PIC X(15). 001400 FD DEPT-2. 001500 01 DEP1-2-REC PIC X(15). 001600 FD ALL-DEPT. 001700 01 ALL-DEPT-REC PIC X(15). 001800 SD WORK-FILE. 001900 01 WORK-REC. 002000 05 EMP-ID-WRK PIC 9(05). 002100 05 EMP-NAME-WRK PIC X(10). 002200 PROCEDURE DIVISION. 002300 MAIN-PARA. 002400 MERGE WORK-FILE. 002500 ON ASCENDING KEY EMP-ID-WRK 002600 USING DEPT-1,DEPT-2 002700 GIVINS ALL-DEPT. 002800 STOP RUN.

JCL Step to execute above COBOL

//STEP001 EXEC PGM=MERGEPGM //DEPT1 DD DSN=USERID.DEPT1,DISP=SHR //DEPT2 DD DSN=USERID.DEPT2,DISP=SHR //ALLDEPT DD DSN=USER.DATA.OUT, // DISP=(NEW,CATLG,DELETE), // UNIT=TEST,SPACE=(CYL,(25,5),RLSE), // DCB=(LRECL=15,BLKSIZE=1500,RECFM=FB) //WRKDD DD DSN=USER.DATA.TEMP, // DISP=(,DELETE,DELETE), // UNIT=WORK,SPACE=(CYL,(25,5),RLSE), // DCB=(LRECL=15,BLKSIZE=1500,RECFM=FB)






© copyright mainframebug.com
Privacy Policy