MERGE Statement
The MERGE
statement merges multiple files with identical key layouts into a single file.
General Format:
MERGE sort-file-1
{ ON {ASCENDING} KEY { sort-key-1 … } …
{DESCENDING}
[COLLATING SEQUENCE {IS alphabet-name-1}
[WITH DUPLICATES IN ORDER]
USING file-2 { file-3}...
GIVING {file-4} …
{OUTPUT PROCEDURE IS proc-1 [{THROUGH} proc-2]}
{THRU }
Syntax:
sort-file-n
is a file described in the File Section with anSD
.sort-key-n
is a data element that has been named as a key for theSORT
.alphabet-name
is a user defined word.file-n
is a file described in the File Section with anFD
.proc-n
is the name of a procedure or section in the program.
General Rules:
Overview of the MERGE
Statement.
The MERGE
Statement is provided with a number of input files (USING
files), all sorted on identical key layouts. The MERGE
statement OPEN
's all of the USING
files, READ
's the first record of each USING
file, compares the records on the KEY
, selects a record according to the KEY
information, and WRITE
's it to the temporary merge-file. When all of the records have been merged into the merge-file, they are transferred into the output file(s) either through the GIVING
clause or the RETURN
statement in an OUTPUT PROCEDURE
.
When all of the records have been merged into the merge-file, there are two ways to return records from the merge-file into an output file. The simplest way, from a syntactic standpoint, is with the USING
/GIVING
syntax. This requires a statement, such as:
MERGE ALLSTAR-MERGE
*MAJOR SORTKEY
ON ASCENDING KEY MEMBER-LAST-NAME
*MINOR SORTKEY
ON DESCENDING KEY MEMBER-TEAM
*DUPLICATES SORTED IN THE ORDER ACQUIRED
WITH DUPLICATES IN ORDER
USING BASEBALL-FILE, HOOP-FILE
GIVING ALLSTAR-FILE.
In this case, the MERGE
statement uses the ALLSTAR-MERGE
file as the temporary sort file, into which the records are written before they are transferred to the GIVING
file.
A slightly more complex alternative, which provides the user with more flexibility involves using the OUTPUT PROCEDURE
syntax. This requires a statement, such as:
77 OUTPUT-MERGE-AT-END PIC X.
88 EOF-MERGE-FILE VALUE "Y".
.....
MERGE ALLSTAR-MERGE
*MAJOR SORTKEY
ON ASCENDING KEY MEMBER-LAST-NAME
*MINOR SORTKEY
ON DESCENDING KEY MEMBER-TEAM
*DUPLICATES SORTED IN THE ORDER ACQUIRED
WITH DUPLICATES IN ORDER
USING BASEBALL-FILE, HOOP-FILE
OUTPUT PROCEDURE OUTPUT-PROC.
.....
OUTPUT-PROC SECTION.
OUTPUT-PROCESS.
OPEN OUTPUT ALLSTAR-FILE.
PERFORM UNTIL EOF-MERGE-FILE
RETURN ALLSTAR-MERGE
AT END MOVE "Y" TO OUTPUT-MERGE-AT-END
NOT AT END
WRITE ALLSTAR-INFO FROM MERGE-DATA
END-RETURN
END-PERFORM.
In this case, the MERGE
statement uses the ALLSTAR-MERGE
file as the temporary sort file, uses the OUTPUT PROCEDURE
as an area where processing can be done, and controls the writing to the output file through the use of the RETURN
statement.
Note that while the input files, and output file are described in the FILE SECTION
of the program with FD
’s, the ALLSTAR-MERGE
file must be described in the FILE SECTION
of the program with an SD
.
General Rules:
- The
MERGE
statement performs the equivalent ofOPEN INPUT
for all of theUSING
files. After theMERGE
is complete, theMERGE
statementCLOSE
's theUSING
andGIVING
files. Sort-file-1
must be described with anSD
in theFILE SECTION
.- The
USING
andGIVING
files must be described withORGANIZATION LINE SEQUENTIAL
, orORGANIZATION BINARY SEQENTIAL
, and must be described with anFD
in theFILE SECTION
. - The order in which the
KEY
clauses are listed indicates the order in which the sort keys are applied. The firstKEY
listed is the primary key, the second key listed is the second key, etc... - The
ASCENDING KEY
clause causes values to sort from lowest to highest. - The
DESCENDING KEY
clause causes values to sort from highest to lowest. - If an
ASCENDING
/DESCENDING
clause has been used, it will be applied to subsequentKEY
clauses until an alternativeDESCENDING
/ASCENDING
clause is used. - The
COLLATING SEQUENCE
clause names the alphabet which is used for purposes of orderingKEY
data in theMERGE
. Usage of theCOLLATING SEQUENCE
clause in aMERGE
statement overrides the naming of aPROGRAM COLLATING SEQUENCE
in theobject-computer
paragraph. - The
MERGE
statement writes duplicates in the order in which they are encountered. That is, if there are twoUSING
files, and the current records in each file have duplicate keys, then the order of the listing of theUSING
files determines which record is written first. - Files listed in the
USING
clause may not beOPEN
when theMERGE
statement executes. TheMERGE
statementOPEN
's the files. - All of the input files must be sorted on identical keys, as described by the
KEY
clause of theMERGE
statement. - Files listed in the
GIVING
clause may not beOPEN
when theMERGE
statement executes. TheMERGE
statementOPEN
's the files. - The
OUTPUT PROCEDURE
is executed after all of the files have been merged into the temporary merge file. - Inside the
OUTPUT PROCEDURE
, theRETURN
statement is used to transfer records from the merge file to the output file. - When the
OUTPUT PROCEDURE
is finished, theMERGE
statement closes all files. - The
MERGE
statement updates file status variable that is defined for merge-file after performingOPEN
,READ
,WRITE
, andCLOSE
operations. File Status errors may be detected inDECLARATIVES
.
Code Sample:
IDENTIFICATION DIVISION.
PROGRAM-ID. MERGE-1.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
*MERGE INPUT FILE
SELECT BASEBALL-FILE ASSIGN TO "BASEBALLSTARS.TXT"
ORGANIZATION IS LINE SEQUENTIAL
ACCESS IS SEQUENTIAL
FILE STATUS IS BASEBALL-STAT.
*MERGE INPUT
SELECT HOOP-FILE ASSIGN TO "HOOPSTARS.TXT"
ORGANIZATION IS LINE SEQUENTIAL
ACCESS IS SEQUENTIAL
FILE STATUS IS HOOP-STAT.
*MERGE OUTPUT FILE
SELECT ALLSTAR-FILE ASSIGN TO "ALLSTARS.TXT"
ORGANIZATION IS LINE SEQUENTIAL
ACCESS IS SEQUENTIAL
FILE STATUS IS ALLSTARS-STAT.
*MERGE FILE (SD)
SELECT ALLSTAR-MERGE ASSIGN TO "MERGE-WORK".
DATA DIVISION.
FILE SECTION.
FD BASEBALL-FILE.
01 BASEBALL-INFO PIC X(40).
FD HOOP-FILE.
01 HOOP-INFO PIC X(40).
FD ALLSTAR-FILE.
01 ALLSTAR-INFO PIC X(40).
SD ALLSTAR-MERGE.
01 MERGE-DATA.
05 MEMBER-FIRST-NAME PIC X(10).
05 MEMBER-LAST-NAME PIC X(15).
05 MEMBER-TEAM PIC X(15).
WORKING-STORAGE SECTION.
77 BASEBALL-STAT PIC XX.
77 HOOP-STAT PIC XX.
77 ALLSTARS-STAT PIC XX.
77 DUMMY PIC X.
PROCEDURE DIVISION.
STAR-MERGE.
MERGE ALLSTAR-MERGE
*MAJOR SORTKEY
ON ASCENDING KEY MEMBER-LAST-NAME
*MINOR SORTKEY
ON DESCENDING KEY MEMBER-TEAM
*DUPLICATES SORTED IN THE ORDER ACQUIRED
WITH DUPLICATES IN ORDER
USING BASEBALL-FILE, HOOP-FILE
GIVING ALLSTAR-FILE.
DISPLAY "MERGE-1 FINISHED!" LINE 10 COL 10.
ACCEPT DUMMY LINE 10 COL 30.
STOP RUN.
→baseballstars.txt
BABE RUTH YANKEES
LOU GEHRIG YANKEES
JOE DIMAGGIO YANKEES
ENOS SLAUGHTER CARDINALS
DOMINIC DIMAGGIO RED SOX
JOHNNY BENCH REDS
BARRY BONDS GIANTS
HANK AARON BRAVES
→hoopstars.txt
LARRY BIRD CELTICS
MAGIC JOHNSON LAKERS
BILL RUSSELL CELTICS
JULIUS ERVING SIXERS
SPUD WEBB HAWKS
MOSES MALONE SIXERS
KC JONES CELTICS
MICHAEL JORDAN BULLS