CPAN 320 COBOL

Lecture 15

Indexed and Relative File Processing

Methods for organizing Disk Files:

  • Sequential File Organization:

Records are stored in sequence by a key field such as customer number. In order to read a specific record, the program must read all the records prior to that record.

In order to update a sequential master file we have to use one of two methods:

Create a new master file using the previous master file and the transaction file of changes as input.

Rewrite the master records that have changed.

  • Indexed File Organization:

With an indexed file organization we have two files. The data file which is created in sequence but can be accessed randomly, and the index file, which contains the value of each key field and the disk address of the record with the corresponding key field.

To access the data file randomly we look up the key field in the index file to find the address of the required record, then the record in the index data file is access directly.

With indexed files we have a choice to access the records sequentially, randomly or both.

  • Relative File Organization:

The key field of each record is used to calculate a disk address where the record is written to a relative file, then the records are accessed randomly using this key field will determine the physical location of the record in the file.

We will cover in this lecture processing indexed disk files only using VSAM, which is IBM’s implementation for processing, indexed files. COBOL 85 was the first version of COBOL that has standardized indexed files processing.

Processing indexed disk files:

Indexed files must be created using a program; they can’t be created using a text editor.
In order to create in index file from a sequential input file, records are read from the sequential input file in sequence by a key field and then the index records are written to the index file. Once the index file is created, records can be accessed randomly.

To create an index file we use SELECT statement, which has the following format:

SELECT file-name-1 ASSIGN TO implementation-name-1

[ORGANIZATION IS] INDEXED

[ACCESS MODE IS SEQUNETIAL]

RECORD KEY IS data-name-1

Ex1:

SELECT MASTER-FILE ASSIGN TO 'A:\MD.DAT'

ORGANIZATION IS INDEXED

ACCESS IS SEQUENTIAL

RECORD KEY IS INDEX-SS-NO.

Even though the file is created sequentially, we must indicate that this is an index file. This instruct the computer to create an index so that we can access the file randomly after its been created.

ACCESS clause is used to specify which method will be used in the program (default is sequential). Indexed file are always created sequentially.

RECORD KEY clause indicates the key field within the disk record that will be used to form the index. This field must be in the same physical location in each indexed record (usually it is the first field).

To write a record into an index file we use write statement, which has the following format:

WRITE record-name-1 [FROM identifier-1]

[INVALID KEY imperative- statement-1]

INVALID KEY clause is used to test two possible errors:

1-A key field that is not in sequence.

2-A key field that is the same is an existing one.

Ex2:

WRITE INDEX-REC

INVALID KEY PERFORM ERR-RTN

Updating an indexed file RANDOMLY:

In order to update an indexed we need two files: transaction file and the master index file itself. To update an index file randomly perform the following steps:

  • Read the transaction record from the transaction file.
  • We open the index file as I-O, for input-output, because we will read records from it and write records back to it. For example:

OEPN I-O INDEX-FILE.

  • Move the transaction key field to the indexed record’s key field.
  • When read from the indexed file is executed, the computer will look-up and access the specific record in the master file. If no match is found, then an error routine must be performed.

SELECT statement for accessing the master file randomly have the following format:

SELECT file-name-1 ASSIGN TO implementation-name-1

[ORGANIZATION IS] INDEXED

ACCESS MODE IS RANDOM

RECORD KEY IS data-name-1

When reading an index file randomly, READ statement doesn’t have to be tested for and AT END condition because the master indexed file is not read in sequence.

  • Make the changes to the master record directly or by moving transaction data.
  • REWRITE the master record. REWRITE statement has the following format:

REWRITE record-name-1 [FRPM identifier-1]

[INVALID KEY imperative-statement-1 ]

[NOT INVALID KEY imperative-statement-2 ]

[END-REWRITE]

Insert new record to an indexed file:

If we read a record from a transaction file and we want to add it to an indexed master file we use WRTIE statement.

Deleting an existing record from an indexed file:

DELETE statement is used to delete an existing indexed master record after this record is been looked up. The format of DELETE statement is:

DELETE index-file-name-1 RECORD

[INVALID KEY imperative-statement-1]

[NOT INVALID KEY imperative-statement-2]

[END-DELETE]

Ex3:

MOVE SS-NO-IN TO INDEX-SS-NO

READ MASTER-FILE

INVALID KEY

PERFORM ERR-RTN

NOT INVALID KEY

DELETE MASTER-FILE RECORD

INVALID KEY PERFORM ERR-DELETE

END-DELETE

END-READ

Where SS-NO-IN is an input field obtained either from a transaction file or form the screen in interactive processing.

Accessing an index file dynamically:

If you wish to access an index file both randomly and sequentially in a single program, we use ACCESS IS DYNAMIC clause with SELECT statement.

Using ALTERNATE RECORD KEY clause:

Indexed files may be created and accessed by more than one key field. To enable a file to be accessed using more than one key field we need to use ALTERNATE RECORD KEY clause in the SELECT statement as follow:

SELECT file-name-1

ASSIGN TO implementation-name-1

ORGANIZATION IS INDEXED

SEQUENTAIL

ACCESS MODE IS RAMDOM

DYNAMIC

RECORD KEY IS data-name-1

[ALTERNATE RECORD KEY IS data-name-2]

[ WITH DUPLICATES]

Note that:

  • More than one ALTERNATE RECORD KEY can be used.
  • WITH DUPLICATES clause is used to indicate that an ALTERNATE RECORD KEY doesn’t need to be unique.
  • A record can be accessed either by its RECORD KEY, or by any of its ALTERNATE RCORD KEY.

Ex4:

SELECT INDEX-FILE ASSIGN TO 'A:\MYFILE.DAT'

ORGANIZATION IS INDEXED

ACCESS IS SEQUENTIAL

RECORD KEY IS INDEX-SER-NO

ALTERNATE RECORD KEY IS INDEX-DEP-NO

WITH DUPLICATES.

Then In order to access the INDEX-FILE by the key field we code:

MOVE SER-NO-IN TO INDEX-SER-NO

READ INDEX-FILE

INVALID KEY PERFORM ERR-RTN

END-EREAD

Or we can access this file using the alternative key field by coding:

MOVE DEP-NO-IN TO INDEX-DEP-NO

READ INDEX-FILE

KEY IS INDEX-DEP-NO

INVALID KEY PERFORM ERR-RTN

END-EREAD

START statement:

START statement enables to process an indexed file sequentially from different locations. The general format of this statement is:

IS EQUEL TO

IS =

IS GREATER THAN

START file-name-1 KEY IS > data-name-1

IS NOT LESS THAN

IS NOT <

IS GREATER THAN OR EQUAL TO

IS >=

[INVALID KEY imperative-statement-1]

[NOT INVALID KEY imperative-statement-2]

[END-START]

Notice that START locates the desired record only. To read this record we must use READ statement.

Ex5:

MOVE 10 TO INDEXS-SER-NO

START INDEX-FILE

INVALID KEY PERFORM ERR-RTN

END-START

READ INDEX-FILE

AT-END PERFORM END-JOB

NOT AT END PERFORM CALC-RTN

END READ

Notice in this example that we are using AT-END option because we are reading the file in sequence.

The following rules must be applied when using START statement:

  • The file must be accessed with:

a - ACCESS IS SEQUENTIAL for reading records in sequence by the

RECORD KEY.

b - ACCESS IS DYNAMIC for reading records in sequence by an

ALTERNATE RECORD KEY.

  • The file must be opened as either input or I-O.
  • If the key phrase is omitted, the relational operator ‘IS EQUAL TO’ is implied, and the primary record key is assumed to be the key of reference.

FILE STATUS clause:

If an INVALID KEY clause is executed, then a write error has occurred. FILE STATUS clause can be used with the SELECT statement to determine the exact type of input or output error.

For the possible values that may be placed in the FILE STATUS field see the textbook Pages 663-664.

Ex6:

SELECT INDEX-FILE ASSIGN TO 'A:\MYFILE.DAT'

ORGANIZATION IS INDEXED

ACCESS IS SEQUENTIAL

RECORD KEY IS INDEX-SER-NO

FILE STATUS IS WS-STATUS.

Where WS-STATUS must be defined in the WORKING-STORAGE SECTION as:

WS-STATUSPIC X(2).

Then in the procedure division we may code:

WRITE INDEX-REC

INVALID KEY PERFORM ERR-RTN

END-WRITE

IF WS-STATUS = ‘00’

PERFORM OK-RTN

END-IF

Where ERR-RTN may be coded as

ERR-RTN.

IF WS-STATUS = ‘21’

PERFORM KEY-NOT-IN-SEQUENCE

END-IF

IF WS-STATUS = ‘22’

PERFORM DUPLICATE-KEY

END-IF

1