In this topic, we described about the below sections -
The MERGE statement combines two or more identically sequenced files based on one or more keys and creates an output file with records available in merged order.
The files should have been already sorted according to an identical set of ascending/descending keys. A MERGE statement can code anywhere in the procedure division except in a declarative section.
The name given in the SD entry. No file-name can be repeated in the MERGE statement. When the MERGE statement is executed, all records contained in file-name-2, file-name-3, ... are accepted and then merged according to the keys specified.
ASCENDING/DESCENDING KEY phrase -
Specifies the processing order that records are to be processed in an ascending or descending sequence.
Specifies a KEY data item that used for merging.Multiple data-names can be specified after the word KEY. The leftmost data-name is the major key, the next data-name is the next most significant key and so forth.
- A specified key data item must be physically located in the same position and have the same data format in each input file.
- If file-name-1 has more than one record formats, the KEY data items should be defined in only one among all record formats.
- If file-name-1 contains variable-length records, all of the KEY data-items must be contained within the minimum records size specified for file-name-1.
- KEY data items must not a data item or subordinate to a data item that contains an OCCURS clause.
- KEY data items cannot be -
- Variably located.
- Group items that contain variable-occurrence data items.
- Category numeric described with usage NATIONAL (national decimal type).
- Category external floating-point described with usage NATIONAL (national floating-point).
- KEY data items can be qualified.
- KEY data items can be any of the following data categories -
- Alphabetic, alphanumeric, alphanumeric-edited.
- Numeric (except numeric with usage NATIONAL).
- Numeric-edited (with usage DISPLAY).
- Internal floating-point or display floating-point.
The direction of the merge operation depends on the specification of the ASCENDING or DESCENDING keywords as follows -
- When ASCENDING is specified, the sequence is from the lowest key value to the highest key value.
- When DESCENDING is specified, the sequence is from the highest key value to the lowest key value.
COLLATING SEQUENCE phrase -
Specifies the collating sequence to be used in alphanumeric comparisons for the KEY data items in this merge operation. When the COLLATING SEQUENCE phrase is specified, the indicated collating sequence is used for key data items comparisons of alphabetic, alphanumeric, alphanumeric-edited, external floating-point and numeric-edited categories.
When the COLLATING SEQUENCE phrase is not specified, the key comparisons are performed according to the rules for comparison of operands. The COLLATING SEQUENCE phrase has no effect for keys that are not alphabetic or alphanumeric.
Must be specified in the ALPHABET clause of the SPECIAL-NAMES paragraph.
USING phrase -
file-name-2, file-name-3, ...
Specifies the input files. During the MERGE operation, all the records from file-name-2, file-name-3, ... are transferred to file-name-1. When MERGE statement is executed, these files should not be opened. The input files are automatically opened, read and closed.
All input files must specify sequential or dynamic access mode and be defined in FD entries in the data division. If file-name-1 contains variable-length records, the size of the records contained in the input files (file-name-2, file-name-3, ...) must be neither less than the smallest record nor greater than the largest record of the file-name-1. If file-name-1 contains fixed-length records, the size of the records contained in the input files must not be greater than the largest record of file-name-1.
GIVING phrase -
Specifies the output files. When the GIVING phrase is specified, all the merged records in file-name-1 are automatically transferred to the output files (file-name-4, ...). All output files must specify sequential or dynamic access mode and be defined in FD entries in the data division.
When MERGE statement is executed, the output files (file-name-4, ...) should not be opened. The output files are automatically opened, read and closed. If the output files (file-name-4, ...) contain variable-length records, the size of the records contained in file-name-1 must be neither less than the smallest record nor greater than the largest record of output files. If the output files contain fixed-length records, the size of the records contained in file-name-1 must not be greater than the largest record described for the output files.
OUTPUT PROCEDURE phrase -
Specifies the name of a procedure used to select or modify output records from the merge operation.
Specifies the first (or only) section or paragraph in the OUTPUT PROCEDURE.
Identifies the last section or paragraph in the OUTPUT PROCEDURE. The OUTPUT PROCEDURE can consist of any statements needed to select, modify or copy the records that are made available by the RETURN statement in merged order from the file referenced by file-name-1.