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.
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
1. What underlying mainframe structure typically implements a COBOL indexed file?
2. Which clause must accompany RANDOM or DYNAMIC READ, WRITE, REWRITE, or DELETE operations on an indexed file?
3. What does the START statement do?
4. Which ACCESS MODE allows both direct key lookups and forward sequential reads in the same program?
5. How does an alternate index (AIX) extend a VSAM KSDS?
Was this page helpful?
You May Also Like
Sequential File Processing
Learn how COBOL reads, writes, and updates records in a sequential file, the oldest and most common file organization in mainframe batch systems.
File Status Codes
Learn how COBOL's two-character FILE STATUS codes let a program detect and react precisely to the outcome of every file I/O operation.
Report Writer Basics
Explore COBOL's Report Writer feature, a declarative way to generate formatted, control-broken reports without manually coding line spacing and totals.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics