Friday, September 4, 2009

The SORT Statement

The SORT Statement
Syntax
Format
SORT file-name-1 {ON { ASCENDING
DESCENDING } KEY {data-name-1}...}...

[WITH DUPLICATES IN ORDER] [COLLATING SEQUENCE IS alphabet-name-1]
{ [INPUT PROCEDURE IS procedure-name-1 [ { THROUGH
THRU } procedure-name-2] }
[USING {file-name-2} ...
{ [OUTPUT PROCEDURE IS procedure-name-3 [ { THROUGH
THRU } procedure-name-4] }
[GIVING {file-name-3} ...

Description
The SORT statement is used to sort a data file. The keys to be sorted on must be in the sort file definition, that is in the record associated with the SD (sort description). Two options are available for the input phase of the sort operation as well as two options for the output phase of the operation:
" If the USING phrase is specified, the records to be sorted are copied from the USING file into the SORT file before the data is sorted.
" If the INPUT PROCEDURE phrase is specified, the records to be sorted are entered into the sort file using the RELEASE statement.
" If the GIVING phrase is specified, the records to be sorted are copied in appropriate ASCENDING/DESCENDING KEY order into the GIVING file.
" If the OUTPUT PROCEDURE phrase is specified, the records to be sorted are copied in the appropriate ASCENDING/DESCENDING KEY order into the sort file. Records can be obtained from the file using the RETURN statement.
Tips
1. All of the files referenced must be closed when the SORT statement is executed.
2. The key fields must all be defined in the record description(s) for the sort file.
3. The sort file cannot be opened or closed.
4. If the USING option is used, the USING file must have the same record layout as the sort file.
5. If the GIVING option is used, the GIVING file must have the same record layout as the sort file.