Master Mainframe Technologies - COBOL, JCL, DB2, VSAM, CICS & More
ABEND Codes SQLCODEs File Status Interview Prep Contact
💻 COBOL

COBOL INDEXED Files (VSAM KSDS)

Advanced 🕑 10 min read 👁 0 views

20

Code Example


## COBOL Indexed File Handling

Indexed files (VSAM KSDS - Key Sequenced Data Set) allow both sequential and random access via a primary key, with optional alternate keys.

### File Control Entry

\`\`\`cobol
SELECT file-name ASSIGN TO ddname
    ORGANIZATION IS INDEXED
    ACCESS MODE IS RANDOM/SEQUENTIAL/DYNAMIC
    RECORD KEY IS primary-key
    ALTERNATE RECORD KEY IS alt-key [WITH DUPLICATES]
    FILE STATUS IS ws-file-status.
\`\`\`

### Access Modes

- **SEQUENTIAL**: Process records in key sequence
- **RANDOM**: Direct access by key value
- **DYNAMIC**: Both sequential and random in same program

### File Operations

1. **READ**: Random (by key) or Sequential
2. **WRITE**: Add new record
3. **REWRITE**: Update existing record
4. **DELETE**: Remove record
5. **START**: Position for sequential read

### Key Status Codes

- 00: Success
- 02: Duplicate alternate key
- 21: Sequence error
- 22: Duplicate primary key
- 23: Record not found
- 24: Boundary violation