IBM - VisualAge Cobol for Windows Programming Guide
634 pág.

IBM - VisualAge Cobol for Windows Programming Guide


DisciplinaCobol25 materiais168 seguidores
Pré-visualização50 páginas
The compiler generates an output procedure to open the output file,
return the records, write the records, and close the file. The output file must not be
open when the SORT or MERGE statement begins execution. For example:
SORT Sort-Work-1
ON ASCENDING KEY Sort-Key-1
USING Input-File
GIVING Output-File.
\u201cExample: describing sort and input files for SORT\u201d on page 117
If you want an input procedure to be performed on the sort records before they are
sorted, use SORT . . . INPUT PROCEDURE. If you want an output procedure to be
performed on the sorted records, use SORT . . . OUTPUT PROCEDURE. For example:
SORT Sort-Work-1
ON ASCENDING KEY Sort-Key-1
INPUT PROCEDURE EditInputRecords
OUTPUT PROCEDURE FormatData.
\u201cExample: sorting with input and output procedures\u201d on page 121
Restriction: You cannot use an input procedure with the MERGE statement. The
source of input to the merge operation must be a collection of already sorted files.
However, if you want an output procedure to be performed on the merged
records, use MERGE . . . OUTPUT PROCEDURE. For example:
MERGE Merge-Work
ON ASCENDING KEY Merge-Key
USING Input-File-1 Input-File-2 Input-File-3
OUTPUT PROCEDURE ProcessOutput.
120 Programming Guide
You must define Merge-Work in an SD statement in the FILE SECTION of the DATA
DIVISION, and the input files in FD statements in the FILE SECTION.
Setting sort or merge criteria
To set sort or merge criteria, follow these steps:
1. In the record description of the files to be sorted or merged, define the key or
keys on which the operation is to be performed.
Restriction: A key cannot be variably located.
2. In the SORT or MERGE statement, specify the key fields to be used for sequencing.
You can code keys as ascending or descending. When you code more than one
key, some can be ascending, and some descending.
The leftmost key is the primary key. The next key is the secondary key, and so
on.
You can mix SORT and MERGE statements in the same COBOL program. A program
can perform any number of sort or merge operations. However, one operation
must end before another can begin.
RELATED CONCEPTS
\u201cAppendix D. Complex OCCURS DEPENDING ON\u201d on page 491 (variably located
items)
RELATED REFERENCES
SORT statement (IBM COBOL Language Reference)
MERGE statement (IBM COBOL Language Reference)
Choosing alternate collating sequences
You can sort or merge records in a collating sequence that you specify for
single-byte character keys. The default collating sequence is the collating sequence
specified by the locale setting in effect at compile time. To override the PROGRAM
COLLATING SEQUENCE specified either explicitly (in the OBJECT-COMPUTER paragraph)
or by default, use the COLLATING SEQUENCE phrase of the SORT or MERGE statement.
You can use different collating sequences for each SORT or MERGE statement in your
program.
For DBCS keys, the collating sequence is specified by the locale setting in effect at
run time.
RELATED TASKS
\u201cSpecifying the collating sequence\u201d on page 8
RELATED REFERENCES
SORT statement (IBM COBOL Language Reference)
Example: sorting with input and output procedures
The following example shows the use of an input and an output procedure in a
SORT statement. The example also shows how you can define primary key
SORT-GRID-LOCATION and secondary key SORT-SHIFT in the DATA DIVISION before
using them in the SORT statement.
DATA DIVISION.
. . .
SD SORT-FILE
RECORD CONTAINS 115 CHARACTERS
DATA RECORD SORT-RECORD.
Chapter 8. Sorting and merging files 121
01 SORT-RECORD.
05 SORT-KEY.
10 SORT-SHIFT PIC X(1).
10 SORT-GRID-LOCATION PIC X(2).
10 SORT-REPORT PIC X(3).
05 SORT-EXT-RECORD.
10 SORT-EXT-EMPLOYEE-NUM PIC X(6).
10 SORT-EXT-NAME PIC X(30).
10 FILLER PIC X(73).
. . .
WORKING-STORAGE SECTION.
01 TAB1.
05 TAB-ENTRY OCCURS 10 TIMES
INDEXED BY TAB-INDX.
10 WS-SHIFT PIC X(1).
10 WS-GRID-LOCATION PIC X(2).
10 WS-REPORT PIC X(3).
10 WS-EXT-EMPLOYEE-NUM PIC X(6).
10 WS-EXT-NAME PIC X(30).
10 FILLER PIC X(73).
. . .
PROCEDURE DIVISION.
. . .
SORT SORT-FILE
ON ASCENDING KEY SORT-GRID-LOCATION SORT-SHIFT
INPUT PROCEDURE 600-SORT3-INPUT
OUTPUT PROCEDURE 700-SORT3-OUTPUT.
. . .
600-SORT3-INPUT.
PERFORM VARYING TAB-INDX FROM 1 BY 1 UNTIL TAB-INDX > 10
RELEASE SORT-RECORD FROM TAB-ENTRY(TAB-INDX)
END-PERFORM.
. . .
700-SORT3-OUTPUT.
PERFORM VARYING TAB-INDX FROM 1 BY 1 UNTIL TAB-INDX > 10
RETURN SORT-FILE INTO TAB-ENTRY(TAB-INDX)
AT END DISPLAY 'Out Of Records In SORT File'
END-RETURN
END-PERFORM.
RELATED TASKS
\u201cRequesting the sort or merge\u201d on page 120
Determining whether the sort or merge was successful
The SORT or MERGE statement returns one of the following completion codes after a
sort or merge has finished:
0 Successful completion of the sort or merge
16 Unsuccessful completion of the sort or merge
The completion code is stored in the SORT-RETURN special register. The contents of
this register change after each SORT or MERGE statement is performed.
You should test for successful completion after each SORT or MERGE statement. For
example:
SORT SORT-WORK-2
ON ASCENDING KEY SORT-KEY
INPUT PROCEDURE IS 600-SORT3-INPUT-PROC
OUTPUT PROCEDURE IS 700-SORT3-OUTPUT-PROC.
IF SORT-RETURN NOT=0
DISPLAY \u201cSORT ENDED ABNORMALLY. SORT-RETURN = \u201d SORT-RETURN.
. . .
122 Programming Guide
600-SORT3-INPUT-PROC SECTION.
. . .
700-SORT3-OUTPUT-PROC SECTION.
. . .
If you do not reference SORT-RETURN anywhere in your program, the COBOL run
time tests the return code. If the return code is 16, COBOL issues a run-time
diagnostic message and terminates the run unit (or the thread, in a multithread
environment).
If you test SORT-RETURN for one or more (but not necessarily all) SORT or MERGE
statements, the COBOL run time does not check the return code.
Stopping a sort or merge operation prematurely
To stop a sort or merge operation, use the SORT-RETURN special register. Move the
integer 16 into the register in either of the following ways:
v Use MOVE in an input or output procedure.
Sort or merge processing will be stopped immediately after the next RELEASE or
RETURN statement is performed.
v Reset the register in a declarative section entered during processing of a USING or
GIVING file.
Sort or merge processing will be stopped on exit from the declarative section.
Control then returns to the statement following the SORT or MERGE statement.
Chapter 8. Sorting and merging files 123
124 Programming Guide
Chapter 9. Handling errors
Anticipate possible coding or system problems by putting code into your program
to handle them. Such code is like built-in distress flares or lifeboats. With this code,
output data and files should not be corrupted, and the user will know when there
is a problem.
Your error-handling code can take actions such as handling the situation, issuing a
message, or halting the program. In any event, coding a warning message is a
good idea.
You might create error-detection routines for data-entry errors or for errors as your
installation defines them.
COBOL contains special elements to help you anticipate and correct error
conditions:
v ON OVERFLOW in STRING and UNSTRING operations
v ON SIZE ERROR in arithmetic operations
v Technique handling for input or output errors
v ON EXCEPTION or ON OVERFLOW in CALL statements
RELATED TASKS
\u201cHandling errors in joining and splitting strings\u201d
\u201cHandling errors in arithmetic operations\u201d on page 126
\u201cHandling errors in input and output operations\u201d on page 127
\u201cHandling errors when calling programs\u201d on page 133
Handling errors in joining and splitting strings
During the joining string or splitting of strings, the pointer, used by STRING or
UNSTRING, might fall outside the range of the receiving field. Here a potential
overflow condition exists, COBOL does not let the overflow