COBOL Manual for TNS/E Programs (H06.08+, J06.03+)

Output Procedure
The output procedure is outproc-1 or outproc-1 through outproc-2. The output procedure
must have a RETURN statement. An output procedure can be any procedure needed to select,
modify, or copy the records that the RETURN statement makes available to merge-file one
at a time in merged order.
The range of an output procedure includes statements that execute due to transfer of control
by CALL, EXIT, GO TO, and PERFORM statements in the range of the output procedure and
statements in declarative procedures that execute as a result of execution of statements in the
range of the output procedure. The range of an output procedure must not cause the execution
of any of these statements:
A MERGE, RELEASE, or SORT statement
A RETURN statement that specifies a sort-merge file other than merge-file
A statement that manipulates merge-in-1, merge-in-2, merge-in-n, or merge-out
or uses the record area associated with one of these files
If the MERGE statement is in a section that is not in an independent segment (a segment whose
segment-number is from 00 through 49), then any output procedures must either be totally
within dependent segments or they must be wholly contained in a single independent segment.
If the MERGE statement is in an independent segment (a segment whose segment-number is
from 50 through 99), then any output procedures must either be totally within dependent
segments or they must be wholly contained in the same independent segment that contains
the MERGE statement.
Execution Phases
The execution of a MERGE statement consists of an input-and-merge phase and an output
phase.
Input-and-Merge Phase
The input-and-merge phase transfers records from the input files merge-in-1, merge-in-2,
and merge-in-n to merge-file in the order specified by the merge keys and the collating
sequence alphabet-name.
When the MERGE statement begins execution, merge-file and the input files must be closed
but not locked. The input-and-merge phase performs an implicit OPEN statement on
merge-file and each of the input files. The input-and-merge phase opens each input file in
INPUT mode, performs implicit READ NEXT statements to retrieve their records, and performs
implicit RELEASE statements to release the records to the merge-file.
The MERGE statement determines the merging logic by retrieving the first record from each
input file to form a set of candidate records. After selecting the proper candidate to transfer
to merge-file, the merging logic retrieves the next record from that input file, if any, to
replace it. This process continues until all input files are exhausted. The candidate records are
first ranked in accordance with their values for the most significant key. When two or more
records have equal values for the current key, that subset is then ranked in accordance with
the record values for the next most significant key, and so on.
When all records have been merged, the input-and-merge phase performs an implicit CLOSE
statement on each input file. If the MERGE statement has an OUTPUT PROCEDURE phrase,
the close operations occur after the specified procedure completes execution and returns
control to the MERGE statement.
Output Phase
If the MERGE statement has an OUTPUT PROCEDURE phrase, the output phase transfers the
merged records to the output procedure using an implicit PERFORM statement. The implicit
MERGE 365