beautypg.com

Compaq COBOL AAQ2G1FTK User Manual

Page 281

background image

Using the SORT and MERGE Statements

9.1 Sorting Data with the SORT Statement

9.1.2 Specifying Sort Parameters with the ASCENDING and DESCENDING KEY

Phrases

Use the Data Division ASCENDING and DESCENDING KEY phrases to specify
your sort parameters. The order of data names determines the sort hierarchy;
that is, the major sort key is the first data name entered, while the minor sort
key
is the last data name entered.

In the following example, the hierarchy of the sort is SORT-KEY-1, SORT-KEY-2,
SORT-KEY-3.

SORT SORT-FILE

ASCENDING KEY SORT-KEY-1 SORT-KEY-2
DESCENDING KEY SORT-KEY-3

9.1.3 Resequencing Files with the USING and GIVING Phrases

If you only need to resequence a file, use the USING and GIVING phrases of
the SORT statement. The USING phrase opens the input file, then reads and
releases its records to the sort. The GIVING phrase opens and writes sorted
records to the output file.

Note that you cannot manipulate data with either the USING or the GIVING
phrases.

Consider this SORT statement:

SORT SORT-FILE ON ASCENDING KEY SORT-KEY-1

USING INPUT-FILE GIVING OUTPUT-FILE.

It does the following:

1.

Opens INPUT-FILE

2.

Reads all records in INPUT-FILE and releases them to the sort

3.

Sorts the records in ascending sequence using the data in SORT-KEY-1

4.

Opens the output file and writes the sorted records to OUTPUT-FILE

5.

Closes all files used in the SORT statement

9.1.4 Manipulating Data Before and After Sorting with the INPUT PROCEDURE

and OUTPUT PROCEDURE Phrases

You can manipulate data before and after sorting by using the INPUT
PROCEDURE and OUTPUT PROCEDURE phrases, and sort only some of
the information in a file. For example, these phrases allow you to use only those
input records and/or input data fields you need.

The INPUT PROCEDURE phrase replaces the USING phrase when you want
to manipulate data entering the sort. The SORT statement transfers control to
the sections or paragraphs named in the INPUT PROCEDURE phrase. You then
use COBOL statements to open and read files, and manipulate the data. You use
the RELEASE statement to transfer records to the sort. After the last statement
of the input procedure executes, control is given to the sort, and the records are
subsequently sorted.

After the records are sorted, the SORT statement transfers control to the sections
or paragraphs named in the OUTPUT PROCEDURE phrase. This phrase
replaces the GIVING phrase when you want to manipulate data in the sort.
You can use COBOL statements to open files and manipulate data. You use the

Using the SORT and MERGE Statements 9–3