MERGE Statement COBOL

The MERGE statement combines two or more identically sequenced files on a set of specified keys, and during the process makes records available, in merged order, to an output procedure or to an output file.

MERGE Statement

A MERGE statement may appear anywhere in the Procedure Division except in the declaratives portion.

file-name-1 must be described in a sort-merge file description entry in the Data Division.

data-name-1 may be qualified. data-name-1 must reference either a record-name associated with file-name-1or a data item in a record associated with file-name-1.If more than one record description entry is associated with file-name-1, the data items referenced by different specifications of data-name-1 need not all be associated with the same record description entry.

The data item referenced by data-name-1 must not be a group item that contains a variable-occurrence data item.

file-name-2, file-name-3, and file-name-4 must be described in a file description entry in the Data Division.

No two files specified in any one MERGE statement may reside on the same multiple file reel (or reels).

File-names must not be repeated within the MERGE statement.

The words THRU and THROUGH are synonymous.

No pair of file-names in a MERGE statement may be specified in the same SAME AREA, SAME RECORD AREA, SAME SORT AREA or SAME SORT-MERGE AREA clause.(See the I-O-CONTROL paragraph.)

If the file referenced by file-name-1 contains variable-length records, the size of the records contained in the files referenced by file-name-2 and file-name-3 must not be shorter than the shortest record nor longer than the longest record described for file-name-1. If the file referenced by file-name-1 contains fixed-length records, the size of the records contained in the files referenced by file-name-2 and file-name-3 must not be longer than the longest record described for file-name-1.

If the GIVING phrase is specified and the file referenced by file-name-4 contains variable-length records, the size of the records contained in the file referenced by file-name-1 must not be shorter than the shortest record nor longer than the longest record size specified for file-name-4. If the file referenced by file-name-4 contains fixed length records, the size of the records contained in the file referenced by file-name-1 must not be longer than the fixed record size specified for file-name-4.

General Rules

The general rules applying to the MERGE statement are as follows:

  1. The MERGE statement merges all records contained in the files referenced by file-name-2 and file-name-3 and returns them to an output procedure, or to the file referenced by file-name-4, in an order determined by the ASCENDING and DESCENDING phrases and the values of the data items referenced by the specifications of data-name-1.
  2. The words ASCENDING and DESCENDING apply to each subsequent occurrence of data-name-1 until another word ASCENDING or DESCENDING is encountered.
  3. The data items referenced by the specification of data-name-1 are the key data items that determine the order in which records are returned from the file referenced by file-name-1. The order of significance of the keys is the order in which they are specified in the MERGE statement, without regard to their association with ASCENDING or DESCENDING phrases. The first (or only) key data item is the most significant. Further key data items, if any, are of progressively lesser significance.
  4. To determine the relative order in which two records are returned from the file referenced by file-name-1, the contents of corresponding key data items are compared according to the rules for comparison of operands in a relation condition, starting with the most significant key data item.
    • If the contents of the corresponding key data items are not equal and the key is associated with the ASCENDING phrase, the record containing the key data item with the lower value is returned first.
    • If the contents of the corresponding key data item are not equal and the key is associated with the DESCENDING phrase, the record containing the key data item with the higher value is returned first.
    • If the contents of the corresponding key data items are equal, the determination is made on the contents of the next most significant key data item.
    • If the contents of all the key data items in one record are equal to the contents of the corresponding key data items in another record, the records are returned in the order in which their associated input files are specified in the MERGE statement.If both records are associated with the same file, the order of the records in that file is preserved.
  5. The collating sequence that applies to the comparison of nonnumeric key data items is determined at the beginning of the execution of the MERGE statement in the following order of precedence:
    • The collating sequence established by the COLLATING SEQUENCE phrase, if specified, in that MERGE statement.
    • The collating sequence established as the program collating sequence.
  6. The results of the merge operation are undefined unless the records in the files referenced by file-name-2 and file-name-3 are ordered as described in the ASCENDING or DESCENDING KEY clauses associated with the MERGE statement.
  7. All the records in the files referenced by file-name-2 and file-name-3 in the USING phrase are transferred to the file referenced by file-name-1. At the start of execution of the MERGE statement, the files referenced by file-name-2 and file-name-3 must not be in the open mode.For each of the files referenced by file-name-2 And file-name-3, the execution of the MERGE statement causes the following actions to be taken:
    • The processing of the file is initiated.The initiation is performed as if an OPEN statement with the INPUT phrase had been executed.If an output procedure is specified, this initiation is performed before control passes to the output procedure.
    • The logical records are obtained and released to the merge operation.Each record is obtained as if a READ statement with the NEXT and the AT END phrases had been executed.If the file referenced by file-name-1 contains fixed-length records, any record in the files referenced by file-name-2 and file-name-3 containing fewer character positions than that specified for file-name-1 is space-filled on the right beginning with the first character position after the last character in the record when that record is released to the file referenced by file-name-1.
    • The processing of the file is terminated.The termination is performed as if a CLOSE statement without optional phrases had been executed.If an output procedure is specified, this termination is not performed until after control passes the last statement in the output procedure. These implicit functions are performed such that any associated USE AFTER EXCEPTION procedures are executed.
  8. During the execution of any USE AFTER EXCEPTION procedure implicitly invoked while executing the MERGE statement, no statements may be executed which manipulate the file referenced by file-name-2, file-name-3, or file-name-4, or which access the record area associated with file-name-2, file-name-3, or file-name-4.
  9. The output procedure may consist of any procedure needed to select, modify or copy the records that are made available one at a 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 without the optional PROGRAM phrase, GO TO and PERFORM statements in the range of the output procedure, as well as 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.
  10. If an output procedure is specified, control passes to it during execution of the MERGE statement.The compiler inserts a return mechanism at the end of the last statement in the output procedure.When control passes the last statement in the output procedure, the return mechanism provides for 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.
  11. During the execution of the output procedure, no statement may be executed manipulating the file referenced by, or accessing the record area associated with, file-name-2 or file-name-3.
  12. If the GIVING phrase is specified, all the merged records are written on the file referenced by file-name-4 as the implied output procedure for the MERGE statement.At the start of the execution of the MERGE statement, the file referenced by file-name-4 must not be in the open mode.For each of the files referenced by file-name-4, the execution of the MERGE statement causes the following actions to be taken:
    • The processing of the file is initiated.The initiation is performed as if an OPEN statement with the OUTPUT phrase had been executed.
    • The merged logical records are returned and written onto the file.Each record is written as if a WRITE statement without any optional phrases had been executed.
    • For a relative file, the relative key data item for the first record returned contains the value 1; for the second record returned, the value 2; and so forth.After execution of the MERGE statement, the contents of the relative key data item indicate the last record returned to the file.
    • The processing of the file is terminated.The termination is performed as if a CLOSE statement without optional phrases had been executed.
  13. These implicit functions are performed such that any associated USE procedures are executed; however, the execution of such a USE procedure must not cause the execution of any statement manipulating the file referenced by, or accessing the record area associated with, file-name-4.On the first attempt to write beyond the externally defined boundaries of the file, any USE procedure specified for the file is executed; if control is returned from that USE procedure or if no such USE procedure is specified, the processing of the file is terminated as described above.

  14. Segmentation can be applied to programs containing the MERGE statement. However, the following restrictions apply:
    • If the MERGE statement appears in a section that is not in an independent segment, any output procedure referenced by that MERGE statement must appear:
      1. Totally within non independent segments, or
      2. Wholly contained in a single independent segment.
    • If a MERGE statement appears in an independent segment, any output procedure referenced by that MERGE statement must be contained:
    1. Totally within non independent segments, or
    2. Wholly within the same independent segment as that MERGE statement.

MERGE Statement Example


All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

COBOL Topics