Sort files and merge files must be described with SELECT statements in the Environment Division and SD (Sort Description) entries in the Data Division. For example, see Figure 103. The sort file or merge file described in an SD entry is the working file used during the sort or merge operation. You cannot execute any input/output statements for this file.
To describe files used for input to or output from a sort or merge operation, specify FD (File Description) entries in the Data Division. You can also sort or merge records that are defined only in the Working-Storage Section or Linkage Section. If you are only sorting or merging data items from the Working-Storage Section or Linkage Section and are not using files as input to or output from a sort or merge operation, you still need SD and FILE-CONTROL entries for the sort file or merge file.
Every SD entry must contain a record description, for example:
SD SORT-WORK-1. 01 SORT-WORK-1-AREA. 05 SORT-KEY-1 PIC X(10). 05 SORT-KEY-2 PIC X(10). 05 FILLER PIC X(80).
Figure 103. Environment and Data Division Entries for a Sort Program
5722WDS V5R3M0 030905 LN IBM ILE COBOL CBLGUIDE/SMPLSORT ISERIES1 03/09/15 13:54:42 Page 2 S o u r c e STMT PL SEQNBR -A 1 B..+....2....+....3....+....4....+....5....+....6....+....7..IDENTFCN S COPYNAME CHG DATE 1 000100 IDENTIFICATION DIVISION. 2 000200 PROGRAM-ID. SMPLSORT. 000300 3 000400 ENVIRONMENT DIVISION. 4 000500 CONFIGURATION SECTION. 5 000600 SOURCE-COMPUTER. IBM-ISERIES 6 000700 OBJECT-COMPUTER. IBM-ISERIES 7 000800 INPUT-OUTPUT SECTION. 8 000900 FILE-CONTROL. 001000* 001100* Assign name for a sort file is treated as documentation. 001200* 9 001300 SELECT SORT-WORK-1 10 001400 ASSIGN TO DISK-SORTFILE1. 11 001500 SELECT SORT-WORK-2 12 001600 ASSIGN TO DISK-SORTFILE1. 13 001700 SELECT INPUT-FILE 14 001800 ASSIGN TO DISK-INFILE. 001900 15 002000 DATA DIVISION. 16 002100 FILE SECTION. 17 002200 SD SORT-WORK-1. 18 002300 01 SORT-WORK-1-AREA. 19 002400 05 SORT-KEY-1 PIC X(10). 20 002500 05 SORT-KEY-2 PIC X(10). 21 002600 05 FILLER PIC X(80). 002700 22 002800 SD SORT-WORK-2. 23 002900 01 SORT-WORK-2-AREA. 24 003000 05 SORT-KEY PIC X(5). 25 003100 05 FILLER PIC X(25). 003200 26 003300 FD INPUT-FILE. 27 003400 01 INPUT-RECORD PIC X(100). 003500 003600* . 003700* . 003800* . 003900 28 004000 WORKING-STORAGE SECTION. 29 004100 01 EOS-SW PIC X. 30 004200 01 FILLER. 31 004300 05 TABLE-ENTRY OCCURS 100 TIMES 004400 INDEXED BY X1 PIC X(30). 004500* . 004600* . 004700* . 004800 * * * * * E N D O F S O U R C E * * * * * |
The sort and merge files are processed with SORT or MERGE statements in the Procedure Division. The statement specifies the key field(s) within the record upon which the sort or merge is to be sequenced. You can specify a key or keys as ascending or descending, or when you specify more than one key, as a mixture of the two.
You can mix SORT and MERGE statements in the same ILE COBOL program. An ILE COBOL program can contain any number of sort or merge operations, each with its own independent input or output procedure.
You can perform more than one sort or merge operation in your ILE COBOL program, including:
However, one operation must be completed before another can begin.
(C) Copyright IBM Corporation 1992, 2005. All Rights Reserved.