sign in
 
   
 
 
 
   
  COBOL TUTORIAL FOR BCA STUDENTS OF M G UNIVERSITY  
  UNIT 5: SEQUENTIAL FILES AND FILE PROGRAMS . . .  
   
  SORTING AND MERGING OF FILES  
   
 

In COBOL, there is no specific feature for the sorting of a table. However, its provides a sort verb that can be used to SORT a sequential file. In addition to the sort verb, the MERGE verb can be used to merge several sorted files to create a new file containing the records of these files in the sorting order.


 
  THE SIMPLE SORT VERB  
   
 

The process of sequencing the records in some desired manner is known as sorting. Sorting is done upon some key data item in the record. When a sequential file is to be sorted, its record reside on the file medium and can be accessed to only serially. The simple sort verb requires the naming of three files – the unsorted input file, the sorted output file and the work file. The format of the simple SORT verb is as follows:

 
 

 
 


The work file is to be defined by a sort description entry(SD entry).The format of SD
entry is as follows:

 
 

 
 
The following rules should be taken into considerations while specifying this sort verb:


The input, output as well as the work file are open by the sort statement before the starting begins and are closed by the sort statement itself after the sorting is over.

There can be any number of SORT statement in a program.


The sorting can be done on any number of keys.


All the keys on which the sorting is done, must appear with their description in the record description of file name1.

Keys in the sort statement do not require any qualification.



The SELECT clauses for the work file file- name-1 is SELECT file-name-1 ASSIGN TO hardware-name.

 
   
  THE SIMPLE MERGE VERB  
   
 

Like sorting, the merging of files is frequently required in various commercial application. It is possible to merge two or more files with one MERGE statement. The format of the simple MERGE verb is as follows:

 
 

 
 
The input files to be merged through the MERGE statement are specified in the USING phrase. These files must be sequential files and must be sorted on the merge keys. The rules of the SORT statement in respect of the ASCENDING/DECENDING KEY phrase are also applicable in this case.