VSAM (Virtual Storage Access Method) is IBM's mainframe file access method that provides efficient, indexed access to data. VSAM offers three main organization types: KSDS (Key Sequenced Data Set) for indexed access by key, ESDS (Entry Sequenced Data Set) for sequential access, and RRDS (Relative Record Data Set) for access by relative record number. Understanding VSAM concepts is essential for working with mainframe files that require indexed access, efficient retrieval, and advanced data management capabilities in COBOL programs.
VSAM is an IBM mainframe file access method that provides:
VSAM files must be created and cataloged before use in COBOL programs.
VSAM provides three main organization types:
| Type | Full Name | Access Method | Best Use |
|---|---|---|---|
| KSDS | Key Sequenced Data Set | Indexed by key, random or sequential | Key-based access, updates, deletes |
| ESDS | Entry Sequenced Data Set | Sequential only, in write order | Logs, audit trails, append-only |
| RRDS | Relative Record Data Set | By relative record number (position) | Fixed-size records, position-based access |
KSDS is the most commonly used VSAM organization, providing indexed access by a primary key:
1234567891011121314151617181920212223242526272829303132333435363738394041424344ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT CUSTOMER-FILE ASSIGN TO CUSTOMER.VSAM ORGANIZATION IS INDEXED ACCESS MODE IS RANDOM RECORD KEY IS CUSTOMER-ID FILE STATUS IS CUSTOMER-STATUS. DATA DIVISION. FILE SECTION. FD CUSTOMER-FILE. 01 CUSTOMER-RECORD. 05 CUSTOMER-ID PIC 9(5). 05 CUSTOMER-NAME PIC X(30). 05 CUSTOMER-BALANCE PIC 9(8)V99. 05 CUSTOMER-ADDRESS PIC X(50). WORKING-STORAGE SECTION. 01 CUSTOMER-STATUS PIC XX. 88 FILE-OK VALUE '00'. 88 RECORD-NOT-FOUND VALUE '23'. PROCEDURE DIVISION. MAIN-PARA. OPEN I-O CUSTOMER-FILE IF NOT FILE-OK DISPLAY "Error opening file: " CUSTOMER-STATUS STOP RUN END-IF *> Random access by key MOVE 12345 TO CUSTOMER-ID READ CUSTOMER-FILE INVALID KEY DISPLAY "Customer not found: " CUSTOMER-ID NOT INVALID KEY DISPLAY "Customer: " CUSTOMER-NAME DISPLAY "Balance: " CUSTOMER-BALANCE END-READ CLOSE CUSTOMER-FILE STOP RUN.
Key points for KSDS:
12345678910111213141516171819202122SELECT CUSTOMER-FILE ASSIGN TO CUSTOMER.VSAM ORGANIZATION IS INDEXED ACCESS MODE IS SEQUENTIAL RECORD KEY IS CUSTOMER-ID FILE STATUS IS CUSTOMER-STATUS. PROCEDURE DIVISION. MAIN-PARA. OPEN INPUT CUSTOMER-FILE PERFORM UNTIL END-OF-FILE READ CUSTOMER-FILE AT END SET END-OF-FILE TO TRUE NOT AT END DISPLAY "Customer: " CUSTOMER-NAME *> Process record END-READ END-PERFORM CLOSE CUSTOMER-FILE STOP RUN.
Sequential access reads records in key order, from lowest to highest key value.
ESDS stores records in the order they are written, similar to sequential files:
1234567891011121314151617181920212223242526272829303132333435363738394041424344ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT LOG-FILE ASSIGN TO LOG.VSAM ORGANIZATION IS SEQUENTIAL ACCESS MODE IS SEQUENTIAL FILE STATUS IS LOG-STATUS. DATA DIVISION. FILE SECTION. FD LOG-FILE. 01 LOG-RECORD. 05 LOG-DATE PIC 9(8). 05 LOG-TIME PIC 9(6). 05 LOG-MESSAGE PIC X(100). WORKING-STORAGE SECTION. 01 LOG-STATUS PIC XX. PROCEDURE DIVISION. MAIN-PARA. *> Open for output (append) OPEN EXTEND LOG-FILE *> Write log entry ACCEPT LOG-DATE FROM DATE YYYYMMDD ACCEPT LOG-TIME FROM TIME MOVE "Application started" TO LOG-MESSAGE WRITE LOG-RECORD CLOSE LOG-FILE *> Read sequentially OPEN INPUT LOG-FILE PERFORM UNTIL END-OF-FILE READ LOG-FILE AT END SET END-OF-FILE TO TRUE NOT AT END DISPLAY LOG-MESSAGE END-READ END-PERFORM CLOSE LOG-FILE STOP RUN.
ESDS is useful for log files, audit trails, and any application where records must be preserved in write order and accessed sequentially.
RRDS provides direct access by relative record number (position):
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT LOOKUP-TABLE ASSIGN TO LOOKUP.VSAM ORGANIZATION IS RELATIVE ACCESS MODE IS RANDOM RELATIVE KEY IS RECORD-NUMBER FILE STATUS IS LOOKUP-STATUS. DATA DIVISION. FILE SECTION. FD LOOKUP-TABLE. 01 LOOKUP-RECORD. 05 STATE-CODE PIC X(2). 05 STATE-NAME PIC X(20). 05 STATE-TAX-RATE PIC V999. WORKING-STORAGE SECTION. 01 RECORD-NUMBER PIC 9(4). 01 LOOKUP-STATUS PIC XX. PROCEDURE DIVISION. MAIN-PARA. OPEN I-O LOOKUP-TABLE *> Access record at position 5 MOVE 5 TO RECORD-NUMBER READ LOOKUP-TABLE INVALID KEY DISPLAY "Record not found at position: " RECORD-NUMBER NOT INVALID KEY DISPLAY "State: " STATE-NAME DISPLAY "Tax Rate: " STATE-TAX-RATE END-READ *> Write to position 10 MOVE 10 TO RECORD-NUMBER MOVE 'TX' TO STATE-CODE MOVE 'TEXAS' TO STATE-NAME MOVE 0.0825 TO STATE-TAX-RATE WRITE LOOKUP-RECORD INVALID KEY DISPLAY "Error writing record" END-WRITE CLOSE LOOKUP-TABLE STOP RUN.
RRDS uses RELATIVE KEY (a numeric field) to specify which record position to access. Record numbers start at 1.
VSAM files use standard COBOL I/O statements with some VSAM-specific considerations:
1234567891011*> INPUT - Read only OPEN INPUT VSAM-FILE *> OUTPUT - Write only (creates new file) OPEN OUTPUT VSAM-FILE *> I-O - Read and write (update existing file) OPEN I-O VSAM-FILE *> EXTEND - Append to ESDS OPEN EXTEND VSAM-FILE
12345678910111213141516171819202122*> Random read (KSDS or RRDS) MOVE KEY-VALUE TO RECORD-KEY READ VSAM-FILE INVALID KEY DISPLAY "Record not found" NOT INVALID KEY *> Process record END-READ *> Sequential read READ VSAM-FILE AT END SET END-OF-FILE TO TRUE NOT AT END *> Process record END-READ *> Sequential read with NEXT (explicit) READ VSAM-FILE NEXT AT END SET END-OF-FILE TO TRUE END-READ
123456789101112*> Write new record (KSDS or RRDS) MOVE DATA TO RECORD-FIELDS WRITE RECORD-NAME INVALID KEY DISPLAY "Error: Duplicate key or invalid position" NOT INVALID KEY DISPLAY "Record written successfully" END-WRITE *> Append to ESDS MOVE DATA TO RECORD-FIELDS WRITE RECORD-NAME
12345678910111213*> Update existing record (KSDS or RRDS) MOVE KEY-VALUE TO RECORD-KEY READ VSAM-FILE INVALID KEY DISPLAY "Record not found" NOT INVALID KEY *> Modify record data MOVE NEW-DATA TO RECORD-FIELDS REWRITE RECORD-NAME INVALID KEY DISPLAY "Error updating record" END-REWRITE END-READ
12345678*> Delete record (KSDS or RRDS, not ESDS) MOVE KEY-VALUE TO RECORD-KEY DELETE VSAM-FILE RECORD INVALID KEY DISPLAY "Record not found for deletion" NOT INVALID KEY DISPLAY "Record deleted" END-DELETE
| Aspect | KSDS | ESDS | RRDS |
|---|---|---|---|
| Access method | By key (indexed) | Sequential only | By relative record number |
| Record order | Sorted by key | Write order | By position |
| Random access | Yes (by key) | No | Yes (by position) |
| Sequential access | Yes (in key order) | Yes (in write order) | Yes (by position) |
| Updates | Yes (REWRITE) | No | Yes (REWRITE) |
| Deletes | Yes (DELETE) | No | Yes (DELETE) |
| Keys required | Yes (RECORD KEY) | No | No (uses position) |
| Best for | Key-based lookups, updates | Logs, audit trails | Fixed-size lookup tables |
Follow these best practices:
123456789*> Look up customer by ID MOVE SEARCH-CUSTOMER-ID TO CUSTOMER-ID READ CUSTOMER-FILE INVALID KEY DISPLAY "Customer not found: " SEARCH-CUSTOMER-ID NOT INVALID KEY DISPLAY "Found: " CUSTOMER-NAME *> Process customer record END-READ
12345678910*> Process all records sequentially OPEN INPUT CUSTOMER-FILE PERFORM UNTIL END-OF-FILE READ CUSTOMER-FILE AT END SET END-OF-FILE TO TRUE NOT AT END *> Process CUSTOMER-RECORD END-READ END-PERFORM CLOSE CUSTOMER-FILE
123456789101112*> Update customer balance MOVE CUSTOMER-ID-VALUE TO CUSTOMER-ID READ CUSTOMER-FILE INVALID KEY DISPLAY "Customer not found" NOT INVALID KEY ADD TRANSACTION-AMOUNT TO CUSTOMER-BALANCE REWRITE CUSTOMER-RECORD INVALID KEY DISPLAY "Error updating record" END-REWRITE END-READ
Think of VSAM files like different types of filing systems:
So VSAM is like having different ways to organize and find your files - by name (KSDS), in order written (ESDS), or by position number (RRDS)!
Complete these exercises to reinforce your understanding:
Create a program that opens a KSDS file, accepts a customer ID, and looks up the customer record. Display the customer information if found, or an error message if not found.
Create a program that writes log entries to an ESDS file, then reads them back sequentially and displays all log messages.
Create a program that uses an RRDS file as a lookup table. Read a record from a specific position, update it, and write it back.
Create a program that processes all records in a KSDS file sequentially, calculating totals or performing operations on each record.
Create a program that reads a KSDS record, modifies specific fields, and uses REWRITE to update the record. Handle errors appropriately.
1. What does VSAM stand for?
2. Which VSAM organization provides indexed access by key?
3. Which VSAM organization stores records in write order?
4. Which VSAM organization provides access by relative record number?
5. What access mode is used for random access to KSDS?
6. What must you set before reading a KSDS record with RANDOM access?