The MERGE statement combines two or more identically sequenced files (that is, files that have already been sorted according to an identical set of ascending/descending keys) on one or more keys and makes records available in merged order to an output procedure or output file.
A MERGE statement can appear anywhere in the procedure division except in a declarative section.
No file-name can be repeated in the MERGE statement.
No pair of file-names in a MERGE statement can be specified in the same SAME AREA, SAME SORT AREA, or SAME SORT-MERGE AREA clause. However, any file-names in the MERGE statement can be specified in the same SAME RECORD AREA clause.
When the MERGE statement is executed, all records contained in file-name-2, file-name-3, ... , are accepted by the merge program and then merged according to the keys specified.
This phrase specifies that records are to be processed in an ascending or descending sequence (depending on the phrase specified), based on the specified merge keys.
The following rules apply:
The direction of the merge operation depends on the specification of the ASCENDING or DESCENDING keywords as follows:
If the KEY data item is alphabetic, alphanumeric, alphanumeric-edited, or numeric-edited, the sequence of key values depends on the collating sequence used (see COLLATING SEQUENCE phrase below).
If the KEY data item is described with usage NATIONAL, the sequence of the KEY values is based on the binary values of the national characters.
If the KEY is an external floating-point item with usage DISPLAY, the key is treated as category alphanumeric. The sequence in which the records are merged depends on the collating sequence used.
If the KEY is an external floating-point item with usage NATIONAL, the key is treated as category national.
If the KEY is an internal floating-point item, the sequence of key values is numeric order.
When the COLLATING SEQUENCE phrase is not specified, the key comparisons are performed according to the rules for comparison of operands in a relation condition. For details, see General relation conditions.
When the COLLATING SEQUENCE phrase is specified, the indicated collating sequence is used for key data items of alphabetic, alphanumeric, alphanumeric-edited, external floating-point, and numeric-edited categories. For all other key data items, the comparisons are performed according to the rules for comparison of operands in a relation condition.
This phrase specifies the collating sequence to be used in alphanumeric comparisons for the KEY data items in this merge operation.
The COLLATING SEQUENCE phrase has no effect for keys that are not alphabetic or alphanumeric.
The COLLATING SEQUENCE phrase is valid only when a single-byte ASCII code page is in effect.
When the COLLATING SEQUENCE phrase is omitted, the PROGRAM COLLATING SEQUENCE clause (if specified) in the OBJECT-COMPUTER paragraph identifies the collating sequence to be used. When both the COLLATING SEQUENCE phrase of the MERGE statement and the PROGRAM COLLATING SEQUENCE clause of the OBJECT-COMPUTER paragraph are omitted, the COLLSEQ compiler option indicates the collating sequence used. If COLLSEQ(EBCDIC) is specified, the EBCDIC collating sequence is used. If COLLSEQ(LOCALE) is specified, the collating sequence as indicated by the locale is used. For more information about locales, see Appendix G. Locale considerations.
During the MERGE operation, all the records on file-name-2, file-name-3, ... (that is, the input files) are transferred to file-name-1. At the time the MERGE statement is executed, these files must not be open. The input files are automatically opened, read, and closed. If DECLARATIVE procedures are specified for these files for input operations, the declaratives will be driven for errors if errors occur.
All input files must specify sequential or dynamic access mode and be described 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 described for 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 described for file-name-1. For more information, see the COBOL for Windows Programming Guide.
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 described in FD entries in the data division.
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 described for the 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. For more information, see the COBOL for Windows Programming Guide.
At the time the MERGE statement is executed, the output files (file-name-4, ...) must not be open. The output files are automatically opened, read, and closed. If DECLARATIVE procedures are specified for these files for output operations, the declaratives will be driven for errors if errors occur.
This phrase specifies the name of a procedure that is to select or modify output records from the merge operation.
The OUTPUT PROCEDURE can consist of any procedure needed to select, modify, or copy the records that are made available one at time by the RETURN statement in merged order from the file referenced by file-name-1. The range includes all statements that are executed as the result of a transfer of control by CALL, EXIT, GO TO, PERFORM, and XML PARSE statements in the range of the output procedure. The range also includes all statements in declarative procedures that are executed as a result of the execution of statements in the range of the output procedure. The range of the output procedure must not cause the execution of any MERGE, RELEASE, or SORT statement.
If an output procedure is specified, control passes to it after the file referenced by file-name-1 has been sequenced by the MERGE statement. The compiler inserts a return mechanism at the end of the last statement in the output procedure and when control passes the last statement in the output procedure, the return mechanism provides the termination of the merge and then passes control to the next executable statement after the MERGE statement. Before entering the output procedure, the merge procedure reaches a point at which it can select the next record in merged order when requested. The RETURN statements in the output procedure are the requests for the next record.
The OUTPUT PROCEDURE phrase is similar to a basic PERFORM statement. For example, if you name a procedure in an OUTPUT PROCEDURE, that procedure is executed during the merging operation just as if it were named in a PERFORM statement. As with the PERFORM statement, execution of the procedure is terminated after the last statement completes execution. The last statement in an OUTPUT PROCEDURE can be the EXIT statement (see EXIT statement).
If you use a sort control file to specify control statements, the values specified in the sort control file take precedence over those in the other SORT special registers.
For information, see SORT-CONTROL.
If a MERGE statement is coded in a fixed segment, any output procedure referenced by that MERGE statement must be either totally within a fixed segment or wholly contained in a single independent segment.
If a MERGE statement is coded in an independent segment, any output procedure referenced by that MERGE statement must be either totally within a fixed segment or wholly contained within the same independent segment as that MERGE statement.