ContentsIndexPreviousNext

Highlights for First-Time Users

1. MERGE can be thought of as a specialized version of SORT that has been optimized to give better processing performance than can be achieved using SORT. Bear in mind, however, that MERGE, like SORT, does all of its I/O on disk files and will, therefore, take a variable amount of time to complete, depending on the size of the input files, the number of records in the files and the speed of the disk subsystems.

2. MERGE does not allow the use of an input procedure for manipulating records before they are merged.

3. The files to be merged must have identical record formats and be identically ordered by the same key fields.

4. The result of the merge may be written directly to an output file or made available to an output procedure.

5. The output procedure may not reference any of the input files or their records. You can access the records contained in the input files, in merged order, by using RETURN to fetch records from the merge file.

6. The KEY AREA phrase is a means for defining the merge keys at runtime. When you use KEY AREA, it is not required that the merge file record descriptor contain entries for potential sort keys. Definition of the sort key(s) in the merge file is handled internally by the MERGE routine, using the key table. See syntax rules 2 and 3 and general rules 6 through 10.

7. Summary of the merge process:

a) At the beginning of the MERGE process all input files (in-files) and the temporary merge file (merge-file) are opened and positioned at the head of the file. The input files cannot already be open when the MERGE statement begins.

b) The records of each input file are sequentially READ and released to the merge operation.

c) When all of the records in all of the input files have been read, the input files are closed and MERGE completes its merging process.

d) Following merge processing, if OUTPUT PROCEDURE is specified, control is passed to the output procedure. In the output procedure, each record in the merge file is fetched, in sort order, by the RETURN verb for processing (see the entry for the RETURN statement in this section). When the last statement of the output procedure is executed, control returns to the MERGE statement.

e) If the GIVING phrase is used, the merged records are written to the specified output file(s).

Extended code example 2:

IDENTIFICATION DIVISION.
PROGRAM-ID.  SAMPLE-FILE-MERGE.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
   SELECT WESTERN-REGION-FILE
      ASSIGN TO ....
   SELECT EASTERN-REGION-FILE
      ASSIGN TO ....
   SELECT SOUTHERN-REGION-FILE
      ASSIGN TO ....
   SELECT NATIONAL-PROSPECT-FILE
      ASSIGN TO ....
   SELECT NATIONAL-MERGE-FILE
      ASSIGN TO ....
DATA DIVISION.
FILE SECTION.
FD  WESTERN-REGION-FILE.
01  W-REGION-RECORD        PIC X(30).
FD  EASTERN-REGION-FILE.
01  E-REGION-RECORD        PIC X(30).

FD  SOUTHERN-REGION-FILE.

01  S-REGION-RECORD        PIC X(30).

SD  NATIONAL-MERGE-FILE.

01  SORT-DATA.
    05  PROSPECT-NUMBER    PIC X(5).
    05  PROSPECT-NAME      PIC X(7).
    05  PROSPECT-CLASS     PIC X.
    05  ESTIMATED-VALUE    PIC 9999V9.
    05  SALES-REP-NUMBER   PIC X(3).
    05  SALES-REP-NAME     PIC X(7).
    05  FILLER             PIC XX.
FD  NATIONAL-PROSPECT-FILE.
01  NATIONAL-RECORD        PIC X(30).
WORKING-STORAGE SECTION.
01  FLAGS.
    05   MERGE-LIST-EMPTY   PIC X VALUE "N".
         88 NO-MORE-RECORDS       VALUE "Y".
...
PROCEDURE DIVISION.
PROSPECT-LIST-MERGE-PROCEDURE.
    MERGE NATIONAL-MERGE-FILE
       ON ASCENDING KEY PROSPECT-CLASS
                        SALES-REP-NUMBER
       USING  WESTERN-REGION-FILE,
              EASTERN-REGION-FILE,
              SOUTHERN-REGION-FILE
       OUTPUT PROCEDURE IS PROCESS-PROSPECT-LIST.

PROCESS-PROSPECT-LIST SECTION.
CREATE-NATIONAL-PROSPECT-FILE.
   OPEN OUTPUT NATIONAL-PROSPECT-FILE.
   RETURN NATIONAL-MERGE-FILE
      AT END MOVE "Y" TO MERGE-LIST-EMPTY.
   PERFORM UPDATE-PROSPECT-DATA
      UNTIL NO-MORE-RECORDS.
   CLOSE NATIONAL-PROSPECT-FILE.
   GO TO EXIT-MERGE-OUTPUT-PROCESSING.

UPDATE-PROSPECT-DATA.
*do not write records tagged "TestRep"
   IF SALES-REP-NAME NOT = "TestRep"
*write the record to the output file
      WRITE NATIONAL-RECORD FROM SORT-DATA.
   END-IF.
*fetch the next record
   RETURN NATIONAL-MERGE-FILE
      AT END MOVE "Y" TO MERGE-LIST-EMPTY.

EXIT-MERGE-OUTPUT-PROCESSING.
   EXIT.