COBOL Manual for TNS and TNS/R Programs
Procedure Division Verbs
HP COBOL Manual for TNS and TNS/R Programs—522555-006
9-124
MERGE
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 PERFORM retrieves the merge-file records by
executing implicit RETURN statements. When the implicit PERFORM statement
finishes executing, control returns to the merge operation.
If the MERGE statement has a GIVING phrase, the output phase transfers the
merged records to one or more merge-out files. When the MERGE statement
begins execution, each merge-out file must be closed but not locked. The output
phase uses implicit OPEN statements to open each merge-out file in OUTPUT
mode. Then the output phase executes implicit RETURN statements to retrieve
records from the merge file and implicit WRITE statements to release them to the
merge-out files. Finally, the output phase uses implicit CLOSE statements to
close the merge-out files.
Regardless of whether the MERGE statement has an OUTPUT PROCEDURE
phrase or a GIVING phrase, the merge operation uses an implicit CLOSE
statement to close merge-file after the output phase ends. Then the MERGE
statement terminates execution.
The implicit OPEN and CLOSE statements are equivalent to OPEN and CLOSE
statements without optional phrases.