1. MERGE

The MERGE statement merges sequenced files. Records of each file must be sorted according to the keys.

figure pd merge
MERGE Statement Format

The following items are specified in the statement.

  • file-name-1

    • The name described in the SD entry.

  • ASCENDING KEY and DESCENDING KEY phrases

    • Specifies an ascending or descending order for the merge keys.

      Item Description

      data-name-1

      Specifies a KEY data item to be used in the MERGE statement. data-name-1 must be a data item included in a record for file-name-1.

      The first specified key is the major key, and the next data item is the second major key.

      • A KEY data item must be located in the same position and share the same data format in each input file. It does not need to have the same data-name.

      • If one or more records are described in file-name-1, the KEY data items need to be described in one of the records.

      • KEY data items cannot include an OCCURS clause, and cannot belong to an item containing an OCCURS clause.

      • KEY data items can be qualified.

        KEY data items can be one of the following data categories.

        • Alphabetical, alphanumeric, alphanumeric-edited

        • Numeric

        • Numeric-edited (with usage DISPLAY)

        • Internal floating-point or display floating-point

  • COLLATING SEQUENCE phrase

    • Specifies the collating sequence used in alphanumeric comparison of key data items in a merge statement.

      Item Description

      alphabet-name-1

      Must be defined in the ALPHABET clause of the SPECIAL-NAMES paragraph.

  • USING phrase

    • Specifies the input files.

      Item Description

      file-name-2,…​

      Specifies the input files. If a USING phrase is used, all records of input files(file-name-2,…​,) are automatically transmitted to file-name-1.

      All input files must be described in the FD entries of the data division.

  • GIVING phrase

    • Specifies the output files.

      Item Description

      file-name-3,…​

      Specifies the output files.

      If a GIVING phrase is used, all sorted records are automatically transmitted to the output files. All output files must be described in the FD entries of the data division.

  • OUTPUT PROCEDURE phrase

    • Specifies the procedure name to select and modify output records after the merge operation.

      Item Description

      procedure-name-1

      Specifies the first (or unique) section or paragraph of the output procedure.

      procedure-name-2

      Specifies the last section or paragraph of the output procedure.

    • Because the OUTPUT PROCEDURE phrase includes a RETURN statement, records are transmitted one at a time from the file referenced by file-name-1. Various phrases like OPEN, WRITE, and CLOSE can be used to select, copy, or modify output records.

After a MERGE statement is executed, all records included in file-name2, file-name3, …​ are merged according to the specified keys.