CHAPTER 14 SORTING AND MERGING Structured COBOL Programming, Stern & Stern, 9th Edition THE SORT STATEMENT Records in files frequently must be sorted into specific sequences for updating, answering inquiries, or generating reports. Sorting is a common procedure used for arranging records into a specific order so that sequential processing can be performed. There are two techniques used for sorting files processed by COBOL programs. One is to use either a utility or a database management system's sort program. This sort program is completely separate from the COBOL program. Structured COBOL Programming, Stern & Stern, 9th Edition FORMAT OF THE SORT STATEMENT The second alternative is COBOL's SORT verb, which can make it very useful as part of a COBOL program. Often, a COBOL program will SORT a file prior to processing it. A simplified format follows: SORT file-name-1 {ON {DESCENDING}{ASCENDING} KEY data-name-1. .} USING file-name-2 GIVING file-name-3 Structured COBOL Programming, Stern & Stern, 9th Edition Sequencing Records with More Than One SORT Key The SORT verb may be used to sequence records with more than one key field. Suppose that we wish to sort fields in ascending alphabetic sequence by name, within each office. That is: Office number is the major sort field Level number is the intermediate sort field Name is the minor sort field Structured COBOL Programming, Stern & Stern, 9th Edition Sequencing Records with More Than One SORT Key The following is a SORT statement that sorts records into ascending alphabetic NAME sequence within LEVEL-NO within OFFICE- NO: SORT SORT-FILE ON ASCENDING KEY OFFICE-NO ON ASCENDING KEY LEVEL-NO ON ASCENDING KEY NAME USING PAYROLL-FILE-IN GIVING SORTED-PAYROLL-FILE-OUT Structured COBOL Programming, Stern & Stern, 9th Edition ASCENDING / DESCENDING Key If all key fields are to be sorted in ascending sequence we can code using the phrase ON ASCENDING KEY only once: SORT SORT-FILE ON ASCENDING KEY MAJOR-KEY INTERMEDIATE -KEY MINOR-KEY Structured COBOL Programming, Stern & Stern, 9th Edition Coding a Simple SORT Procedure with the USING and GIVING Options There are three major files used in a sort: 1. Input file: File of unsorted input records. 2. Work or sort file: File used to store records temporarily during the sorting process. 3. Output file: File of sorted output records. Structured COBOL Programming, Stern & Stern, 9th Edition Coding a Simple SORT Procedure with the USING/GIVING Options The SORT-FILE is actually assigned to a temporary work area that is used during processing but not saved. Only the unsorted disk file and the sorted output disk file are assigned standard file-names so that they can be permanently stored. FDs are used in the DATA DIVISION to define and describe the input and output files in the usual way. The sort or work file is described with an SD entry. Structured COBOL Programming, Stern & Stern, 9th Edition QUESTIONS! ?? Structured COBOL Programming, Stern & Stern, 9th Edition SELF-TEST 1. Suppose we want EMPLOYEE-FILE records in alphabetic order by NAME within DISTRICT within TERRITORY, all in ascending sequence. The output file is called SORTED-EMPLOYEE- FILE. Complete the following SORT statement: SORT WORK-FILE ... Solution: ON ASCENDING KEY TERRITORY ON ASCENDING KEY DISTRICT ON ASCENDING KEY NAME USING EMPLOYEE-FILE GIVING SORTED-EMPLOYEE-FILE Structured COBOL Programming, Stern & Stern, 9th Edition SELF-TEST 2. How many files are required in a SORT routine? Describe these files.
Solution: Three; input--unsorted; work or sort file-- temporary; output--sorted
Structured COBOL Programming, Stern & Stern, 9th Edition SELF-TEST 3. The work or sort file is defined as an ______ in the DATA DIVISION.
Solution: SD (SORT DESCRIPTION) Structured COBOL Programming, Stern & Stern, 9th Edition SELF-TEST 4. Suppose we have an FD called NET-FILE-IN, an SD called NET-FILE, and an FD called NET- FILE-OUT. We want NET-FILE-OUT sorted into ascending DEPT- NO sequence. Code the PROCEDURE DIVISION entry. Solution: SORT NET-FILE ON ASCENDING KEY DEPT-NO USING NET-FILE-IN GIVING NET-FILE-OUT Structured COBOL Programming, Stern & Stern, 9th Edition SELF-TEST 5. In Question 4, DEPT-NO must be a field defined within the (SD/FD) file
Solution: SD Structured COBOL Programming, Stern & Stern, 9th Edition THE MERGE STATEMENT COBOL has a MERGE statement that will combine two or more files into a single file. Its format is similar to that of the SORT: MERGE file-name-1 {ON {ASCENDING} {DESCENDING} KEY data-name-1. .} USING file-name-2 {file-name-3} . . . {OUTPUT PROCEDURE IS procedure-name-1 GIVING {file-name-4 [{THROUGH}{THRU} procedure- name-2]} Structured COBOL Programming, Stern & Stern, 9th Edition THE MERGE STATEMENT
MERGE MERGE-FILE ON ASCENDING KEY M-EMP-NO USING DOWNSTATE-PAYROLL-FILE UPSTATE-PAYROLL-FILE GIVING MASTER-PAYROLL-FILE. Structured COBOL Programming, Stern & Stern, 9th Edition THE MERGE STATEMENT File-name-1 is a work file designated as an SD. The key field specified as data-name-1, and any subsequent key fields, are defined within the SD. The first key field indicated in the ASCENDING or DESCENDING KEY clause of the MERGE is the major one, followed by intermediate and minor key fields. Rules for ASCENDING/DESCENDING KEY, USING, GIVING, and OUTPUT PROCEDURE are the same as for the SORT. Structured COBOL Programming, Stern & Stern, 9th Edition THE MERGE STATEMENT With the USING clause, we indicate the files to be merged. Unlike the SORT, however, an INPUT PROCEDURE may not be specified with a MERGE statement: you may only process records after they have been merged, not before. The OUTPUT PROCEDURE has the same format as the SORT and may be used with a MERGE to: 1. Flag duplicate records as errors. 2. Ensure duplicate records. Structured COBOL Programming, Stern & Stern, 9th Edition THE MERGE STATEMENT The MERGE statement automatically handles the opening, closing, and input/output (READ/WRITE functions) associated with the files. The files to be merged must each be in sequence by the key field. If ASCENDING KEY is specified, then the merged output file will have records in increasing order by key field. If DESCENDING KEY is specified, the merged output file will have key fields from high to low.