Determining whether the sort or merge was successful

The SORT or MERGE statement returns a completion code of either 0 (successful completion) or 16 (unsuccessful completion) after each sort or merge has finished. The completion code is stored in the SORT-RETURN special register.

About this task

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 "SORT ENDED ABNORMALLY. SORT-RETURN = " SORT-RETURN.
    . . .
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 completion code. If it is 16, COBOL issues a runtime diagnostic message and terminates the run unit (or the thread, in a multithreaded environment). The diagnostic message contains a sort or merge error number that can help you determine the cause of the problem.

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 completion code. However, you can obtain the sort or merge error number after any SORT or MERGE statement by calling the iwzGetSortErrno service; for example:



77  sortErrno    PIC 9(9)   COMP-5.
. . .
    CALL 'iwzGetSortErrno'  USING  sortErrno
. . .

See the related reference below for a list of the error numbers and their meanings.

Related references   
Sort and merge error numbers