Introduction

The records in a Sequential file are organized serially, one after another, but the records in the file may be ordered or unordered. The serial organization of the file and whether the file is ordered or unordered has a significant bearing on how we process the records in the file and what kind of processing we can do.

This tutorial examines the consequences of ordering and organization and reviews some techniques for processing Sequential files.


By the end of this unit you should -

  1. Understand how Sequential files are organized.
  2. Understand the processing limitations imposed by an unordered Sequential file
  3. Be able to add, delete and update records in an ordered Sequential file.

Organization and Access

Two important characteristics of files are Data Organization and Method of Access.

Data organization, refers to the way the records of the file are organized on the backing storage device. COBOL recognizes three main file organizations;

  • Sequential - Records organized serially.
  • Relative - Relative record number based organization.
  • Indexed - Index based organization.

Method of access, refers to the way in which records are accessed. Some organizations are more versatile than others. A file with an organization of Indexed or Relative may still have its records accessed sequentially; but records in a file with an organization of Sequential, cannot be accessed directly.


Sequential organization is simplest file organization in COBOL.

In a Sequential file the records are arranged serially, one after another, like cards in a dealing shoe. The only way to access records in a Sequential file, is serially. A program must start at the first record and read all the succeeding records until the required record is found or until the end of the file is reached.

Sequential files may be Ordered or Unordered (these should be called Serial files). The ordering of the records in a file has a significant impact on the way in which it is processed and the processing that can be done on it.


In an ordered file, the records are sequenced on some field in the record (like StudentId or StudentName etc). In an unordered file, the records are not in any particular order.

Ordered File Unordered File

RecordA
RecordB
RecordG
RecordH
RecordK
RecordM
RecordN

RecordM
RecordH
RecordB
RecordN
RecordA
RecordK
RecordG


Processing unordered Sequential Files

It is easy to add records to an unordered Sequential file because we can simply add them to the end of the file by opening the file for EXTEND (e.g. OPEN EXTEND UnorderedFile). But it is not really possible to delete records from an unordered Sequential file. And it is not feasible to update records in an unordered Sequential file.


To add records to an unordered Sequential File the file must be opened for EXTEND. Opening a file for EXTEND, positions the Next Record Pointer (NRP) at the end of the file, so that when records are written to the file, they are appended to the end.

The animation below demonstrates how records are added to an unordered file. As in all the examples in this tutorial, the records to be added are contained in a transaction file. The transaction file records are applied to the unordered file and the result is an unordered file that has all the transaction file records appended to the end of it.

Click for animation: adding records to an unordered sequential file


While it is easy to add records to an unordered Sequential file it is not really feasible to delete or update records in an unordered Sequential file.

Records in a Sequential file can not be deleted or updated "in situ". The only way to delete records from a Sequential file, is to create a new file which does not contain them; and the only way to update records in a Sequential file, is to create a new file which contains the updated records. Because both these operations rely on record matching, they do not work for unordered Sequential files.


Updates to Sequential files are normally done in batch mode. That is, all the updates are gathered together into a transaction file, and then applied to the target file in one go. The target file is often called the master file.

There are few problems if we are just adding records to the end of a file, but if we want to update or delete records, then there must be some way of identifying the record we want to update or delete. A "key field" is normally used to achieve this.

A key field, is a field in the record whose value is unique to that record. For instance, in a student record, the StudentId is normally used as the key field. Without a key field to identify the record we require, we cannot delete or update records.

When we apply delete or update transaction records to a master file, we compare the key field in the transaction record with that in the master file record. If there is a match, we know we have to update or delete the master file record.

If both files are ordered on the key field, then this record matching operation functions correctly, but if either the transaction or the master file is unordered, record matching cannot work.


Suppose we have an unordered transaction file and an unordered master file. The transaction file contains records which indicate which records in the master file we want to delete. To delete the records we need to create a new master file which does not contain the deleted records.

The animation below demonstrates what happens when we attempt to batch-delete records from an unordered Sequential file.

Click for animation: attempting to batch-delete records from an unordered sequential file


Processing Ordered Sequential Files

An ordered Sequential file, is a file ordered upon some key field. The ordering of the records in the file makes it possible to process an ordered file in ways that are not available to us with unordered files. While it is not really possible to apply batch updates or deletes to an unordered file these are possible with ordered files.


To add records to an ordered file a major consideration is to preserve the ordering. This means that the record must be inserted into the file in the correct position. It can't just be added to the end of the file as it can with unordered files.

As with all changes to ordered Sequential files we can't just insert the records into the existing file. To insert records into the file we have to create a new file that contains the inserted records.

The animation below demonstrates how records are inserted into an ordered Sequential file.

Click for animation: inserting records into an ordered sequential file


The main processing loop from the animation is shown below. Examine the code in this program fragment and then attempt to answer the questions below.

PERFORM UNTIL EndTF AND EndMF
  IF TFKey < MFKey
     MOVE TFRec TO NFRec
     WRITE NFRec
     READ TF etc.
   ELSE
     IF TFKey > MFKey
       MOVE MFRec TO NFRec
       WRITE NFRec
       READ MF etc.
     END-IF
  END-IF
END-PERFORM
Q1
In the program, no allowance has been made for when the key fields are equal. What would it mean if the keys were equal?
Click the arrow for the answer

Since the key field is supposed to be unique this would be a transaction error. It would mean that we were trying to insert a record when a record with that key already exists. Of course, transaction errors do occur so you might want to include code to handle these errors.

Q2
Why do you think the complex condition EndTF AND EndMF has been used to terminate the loop?
Click the arrow for the answer

Since we don't know which file is going to be exhausted first we have to use a condition which says - keep going until the end of both files is reached.

Q3
There does not seem to be any special code to write out the remaining records when one file ends before the other. Can the code above be correct?
Click the arrow for the answer

Yes the code is correct!!! It takes advantage of the fact that when either condition name is set to true, the corresponding record (including its key field) is filled with HIGH-VALUES. Since the key field of the ended file contains the highest possible value, all the keys in the other file will be less than it and so all the remaining records in that file will automatically be written to the new file. I told you using HIGH-VALUES would come in handy.


The animation below demonstrates how to delete records from an ordered Sequential file.

Click for animation: deleting records from an ordered sequential file


Updating a record requires a change to one or more of the fields in the record. Updates to Sequential files are usually collected together and applied to the file in one go - a batch. To update the records in an ordered Sequential file from an ordered batch of transaction records we have to create a new file that contains the updated records.

The animation below demonstrates how to batch-update records in an ordered Sequential files.

Click for animation: batch-updating records in an ordered sequential file

In all the examples in this tutorial the transaction file has contained only records of one type or another. For instance, the transaction file contained either a batch of deletions, or a batch of insertions or a batch of updates. In real life though all these different kinds of transaction record would be gather together into one transaction file.

This raises some interesting problems. For one thing - the record sizes will be different. A deletion record only needs the key field, while an insertion requires the whole record, and an update may be somewhere between these two depending on how many fields we are updating in one go. There may even be different kinds of update record.

In future tutorials we will see how we can define and process files that contain multiple record types.