Menu
CoddyTech

MERGE

Part of the Files and Reports section of Coddy's COBOL Programming journey. Lesson 18 of 22.

MERGE joins files that are already sorted on the same key into one sorted file, such as two branches' transactions in account order:

MERGE WORK-FILE
    ON ASCENDING KEY W-ACCT
    USING EAST-FILE WEST-FILE
    GIVING ALL-FILE.

It reads every USING file at once and always takes the next record in key order. Records with equal keys come out in the order of the files after USING.

MERGE declares its work file with an SD as SORT does, and it takes an OUTPUT PROCEDURE in place of GIVING. It has no input procedure: the files must already be in key order.

challenge icon

Challenge

Easy

JANIN and FEBIN are each in account order. Merge them into ALL-FILE: MERGE WORK-FILE on ascending key W-ACCT, using JAN-FILE and FEB-FILE, giving ALL-FILE.

Try it yourself

       IDENTIFICATION DIVISION.
       PROGRAM-ID. MERGE2.
       ENVIRONMENT DIVISION.
       INPUT-OUTPUT SECTION.
       FILE-CONTROL.
           SELECT JAN-FILE ASSIGN TO JANIN
               ORGANIZATION IS LINE SEQUENTIAL.
           SELECT FEB-FILE ASSIGN TO FEBIN
               ORGANIZATION IS LINE SEQUENTIAL.
           SELECT ALL-FILE ASSIGN TO DISPLAY
               ORGANIZATION IS LINE SEQUENTIAL.
           SELECT WORK-FILE ASSIGN TO MERGEWK.
       DATA DIVISION.
       FILE SECTION.
       FD JAN-FILE.
       01 JAN-REC PIC X(15).
       FD FEB-FILE.
       01 FEB-REC PIC X(15).
       FD ALL-FILE.
       01 ALL-REC PIC X(15).
       SD WORK-FILE.
       01 WORK-REC.
           05 W-ACCT PIC X(5).
           05 FILLER PIC X(5).
           05 W-AMT PIC 9(5).
       PROCEDURE DIVISION.
           *> Write your code here
           STOP RUN.
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Files and Reports