TSO-ISPF JCL COBOL VSAM DB2 CICS Tools Articles Job Portal Forum Quiz Interview Q&A

COBOL - File Access Method


Every file organization (In our previous section, we discussed many file organizations) has different modes of access.

The access mode is used to define the accessing way of the file based on the requirements in the program. In short, we can say, access mode defines how the data is required to read and write from the file.

There are 3 access modes,
  1. Sequential Access
  2. Random Access
  3. Dynamic Access
Now, We will going to see how to define a file in COBOL with correct access mode,

Syntax:

ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
    SELECT [OPTINAL] filename ASSIGN TO ddname.
    ORGANIZATION IS {SEQUENTIAL/INDEXED/RELATIVE}.
    [ACCESS IS {SEQUENTIAL/RANDOM/DYNAMIC}].
    [RECORD KEY IS ws-key].
    [RELATIVE KEY IS ws-rel-rrn].
    [ALTERNATE RECORD KEY IS ws-key {WITH/WITHOUT} DUPLICATES].
    [FILE STATUS IS ws-status].

Let us see each access mode below,

Sequential Access:

As the name indicates, the records in the file can be read sequentially for the sequential access, i.e., one after the other from the beginning.

The method to retrieve the record varies according to the chosen file organization.

In sequential files, records are retrieved in the same way as they were inserted.

Example:

ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
    SELECT file-name ASSIGN TO dd-name
    ORGANIZATION IS SEQUENTIAL
    ACCESS MODE IS SEQUENTIAL.

For indexed files, records are accessed in the order of the key filed selected, beginning at the present position of the file position indicator.

Example:

ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
    SELECT file-name ASSIGN TO dd-name
    ORGANIZATION IS INDEXED
    ACCESS MODE IS SEQUENTIAL
    RECORD KEY IS rec-key1
    ALTERNATE RECORD KEY IS rec-key2.

For relative files, records are accessed in the order of the relative record number.

Example:

ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
    SELECT file-name ASSIGN TO dd-name
    ORGANIZATION IS RELATIVE
    ACCESS MODE IS SEQUENTIAL
    RELATIVE KEY IS rec-key1.

Disadvantages of Sequential Access Mode:
The sequential access mode is very effective when the file has fewer records, but when the file has a huge number of records, it will take too much time to read a specific record from the file.

Random Access:

In the random access mode, we can access the records randomly, i.e., directly by providing the key.

This mode of access is often used only for indexed and relative files.

For indexed files, records are retrieved according to the value you place in a key field. Value can be primary, alternative, or relative. There can be one or more alternate indexes.

Example:

ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT file-name ASSIGN TO dd-name
    ORGANIZATION IS INDEXED
    ACCESS MODE IS RANDOM
    RECORD KEY IS rec-key1
    ALTERNATE RECORD KEY IS rec-key2.

Records are retrieved for relative files based on the value you put in the relative key.

Example:

ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
    SELECT file-name ASSIGN TO dd-name
    ORGANIZATION IS RELATIVE
    ACCESS MODE IS RANDOM
    RELATIVE KEY IS rec-key1.

Dynamic Access:

In the same program, dynamic access allows sequential as well as random access. In dynamic access mode, we can use a file description for both types of processing i.e., random and sequential processing, such as retrieving some records with the help of keys and some records in sequential order.

Example:

ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
    SELECT file-name ASSIGN TO dd-name
    ORGANIZATION IS SEQUENTIAL
    ACCESS MODE IS DYNAMIC
    RECORD KEY IS rec-key1
    ALTERNATE RECORD KEY IS rec-key2.

With relative and indexed files, the dynamic access mode provides you to move between random access mode and sequential access mode when reading a file by using the NEXT phrase on the READ statement.

Example:

ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
    SELECT file-name ASSIGN TO dd-name
    ORGANIZATION IS RELATIVE
    ACCESS MODE IS DYNAMIC
    RELATIVE KEY IS rec-key1.


If you have any doubts or queries related to this chapter, get them clarified from our Mainframe experts on ibmmainframer Community!

Are you looking for Job Change? Job Portal