100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Programming

Indexed File Processing (VSAM)

Understand how COBOL uses indexed files, typically backed by VSAM KSDS on mainframes, to perform fast random and sequential access by key.

File HandlingIntermediate10 min readJul 10, 2026
Analogies

Indexed Organization and VSAM KSDS

ORGANIZATION IS INDEXED declares a file whose records can be retrieved directly by a primary key rather than by scanning from the start. On IBM mainframes this is almost always implemented as a VSAM Key Sequenced Data Set (KSDS), which stores records in key order and maintains an internal index structure (a B-tree-like set of index and control intervals) so the access method can jump directly to the control interval containing the desired key. RECORD KEY IS in the SELECT clause names the field that serves as the primary key, and that field must be part of the record description in the FD, uniquely identifying every record in the file.

🏏

Cricket analogy: A KSDS index is like the ESPNcricinfo player search box — typing 'Kohli' jumps straight to his stats page instead of scrolling through every player alphabetically, just as RECORD KEY enables a direct jump.

Access Modes: SEQUENTIAL, RANDOM, and DYNAMIC

The ACCESS MODE clause controls how a program navigates an indexed file. ACCESS IS SEQUENTIAL processes records in ascending key order using plain READ statements, useful for reports that need every record in key sequence. ACCESS IS RANDOM requires the program to MOVE a value into the RECORD KEY field before each READ, retrieving exactly one record by key, ideal for lookups such as validating a customer ID during data entry. ACCESS IS DYNAMIC combines both: START plus sequential READ NEXT lets a program position at a key and then read forward, while direct READ by key is still available whenever needed, which is the mode most interactive and hybrid batch programs use.

🏏

Cricket analogy: DYNAMIC access is like a commentator who can jump to over 35 to review a specific delivery (random) and then keep calling overs 36, 37, 38 sequentially afterward, combining both access styles.

START, INVALID KEY, and Alternate Indexes

The START statement positions the file at a record whose key satisfies a relational condition (EQUAL, GREATER THAN, NOT LESS THAN, and so on) without actually retrieving the record, which is essential before beginning a DYNAMIC sequential scan from a mid-file point. Every RANDOM or DYNAMIC READ, WRITE, REWRITE, and DELETE against an indexed file must include an INVALID KEY clause, since VSAM signals key-not-found, duplicate-key, or out-of-sequence conditions this way rather than through AT END. Beyond the primary RECORD KEY, VSAM supports alternate indexes (AIX) declared with additional RECORD KEY entries plus WITH DUPLICATES where appropriate, letting the same KSDS be accessed by a secondary field such as an account holder's surname in addition to the primary account number.

🏏

Cricket analogy: START is like a scorer flipping straight to the start of the 30th over in a scorebook without reading its details yet, positioning before actual retrieval, just as START positions without reading.

cobol
ENVIRONMENT DIVISION.
 INPUT-OUTPUT SECTION.
 FILE-CONTROL.
     SELECT ACCOUNT-FILE ASSIGN TO 'ACCTKSDS'
         ORGANIZATION IS INDEXED
         ACCESS MODE IS DYNAMIC
         RECORD KEY IS ACCT-NUMBER
         FILE STATUS IS WS-FILE-STATUS.

 DATA DIVISION.
 FILE SECTION.
 FD  ACCOUNT-FILE.
 01  ACCOUNT-RECORD.
     05  ACCT-NUMBER      PIC 9(9).
     05  ACCT-NAME        PIC X(30).
     05  ACCT-BALANCE     PIC S9(9)V99.

 PROCEDURE DIVISION.
 LOOKUP-ACCOUNT.
     MOVE 000123456 TO ACCT-NUMBER
     READ ACCOUNT-FILE
         INVALID KEY
             DISPLAY 'ACCOUNT NOT FOUND: ' ACCT-NUMBER
         NOT INVALID KEY
             DISPLAY 'BALANCE: ' ACCT-BALANCE
     END-READ.

Always pair FILE STATUS IS with an indexed file's SELECT clause. It gives your program a two-character diagnostic code (e.g., '23' for record not found) that is far more informative for debugging than relying on INVALID KEY alone.

REWRITE against an indexed file can only replace a record with the same key value as the one currently positioned; changing the RECORD KEY on a REWRITE causes an error. To change a key, DELETE the old record and WRITE a new one instead.

  • ORGANIZATION IS INDEXED is typically backed by VSAM KSDS on IBM mainframes.
  • RECORD KEY IS names the primary key field that must be unique across records.
  • ACCESS MODE can be SEQUENTIAL, RANDOM, or DYNAMIC depending on navigation needs.
  • RANDOM/DYNAMIC I/O requires INVALID KEY handling instead of AT END.
  • START positions the file at a key without retrieving the record itself.
  • Alternate indexes (AIX) allow lookup by a secondary field besides the primary key.
  • REWRITE cannot change the record's key value; DELETE and WRITE instead.

Practice what you learned

Was this page helpful?

Topics covered

#Programming#COBOLStudyNotes#IndexedFileProcessingVSAM#Indexed#File#Processing#VSAM#StudyNotes#SkillVeris#ExamPrep