GIVING Phrase

file-name-3, ...
The output files.

When the GIVING phrase is specified, all the sorted records in file-name-1 are automatically transferred to the output files (file-name-3, ... ). At the time the SORT statement is executed, this file must not be open.

If the output files contain variable length records, the size of the records contained in file-name-1 must not be less than the smallest record nor greater than the largest described for the output files. If the output files contain fixed length records, the size of the records contained in file-name-1 must not be greater than the largest record described for the output files.

For each of the files referenced by file-name-3, the execution of the SORT statement causes the following actions to be taken:
  • The processing of the file is initiated. The initiation is performed as if an OPEN statement with the OUTPUT phrase has been executed.
  • The sorted logical records are returned and written onto the file. Each record is written as if a WRITE statement without any optional phrases had been executed. The records overwrite the previous contents, if any, of the file.
    IBM Extension

    IBM Extension If file-name-3 is a logical database file, the records are added to the end of the file. End of IBM Extension

    End of IBM Extension

    If the file referenced by file-name-3 is an INDEXED file then the associated key data-name for that file must have an ASCENDING KEY phrase in the SORT statement. This same data-name must occupy the identical character positions in its record as the data item associated with the prime record key for the file.

    For a relative file, the relative key data item for the first record returned contains the value '1'; for the second record returned, the value '2', and so on. After execution of the SORT statement, the content of the relative key data item indicates the last record returned to the file.

  • The processing of the file is terminated. The termination is performed as if a CLOSE statement without optional phrases had been executed.
Note: When duplicate keys are found when writing to an indexed file, the SORT will terminate and the sorted data in all GIVING files will be incomplete.

These implicit functions are performed such that any associated USE AFTER EXCEPTION/ERROR procedures are executed; however, the execution of such a USE procedure must not cause the execution of any statement manipulating the file referenced by, or accessing the record area associated with, file-name-3.

On the first attempt to write beyond the externally defined boundaries of the file, any USE AFTER STANDARD EXCEPTION/ERROR procedure specified for the file is executed. If control is returned from that USE procedure or if no such USE procedure is specified, the processing of the file is terminated.

All output files must specify sequential or dynamic access mode, and must be described in FD entries in the Data Division.

The output file must be an indexed, relative or sequential file.

The output file should also be created without a keyed sequence access path. When the output file has such a path, the SORT statement cannot override the collating sequence defined in the data description specifications (DDS).