MainframeMaster
MainframeMaster

COBOL Tutorial

Progress0 of 0 lessons

COBOL VSAM Concepts

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.

What is VSAM?

VSAM is an IBM mainframe file access method that provides:

  • Indexed access: Direct access to records by key value
  • Automatic sorting: Records maintained in key sequence
  • Efficient retrieval: Fast access using indexes
  • Multiple organization types: KSDS, ESDS, and RRDS
  • Data integrity: Built-in recovery and consistency features
  • Space management: Automatic space allocation and management

VSAM files must be created and cataloged before use in COBOL programs.

VSAM Organization Types

VSAM provides three main organization types:

VSAM Organization Types
TypeFull NameAccess MethodBest Use
KSDSKey Sequenced Data SetIndexed by key, random or sequentialKey-based access, updates, deletes
ESDSEntry Sequenced Data SetSequential only, in write orderLogs, audit trails, append-only
RRDSRelative Record Data SetBy relative record number (position)Fixed-size records, position-based access

KSDS (Key Sequenced Data Set)

KSDS is the most commonly used VSAM organization, providing indexed access by a primary key:

KSDS Characteristics

  • Indexed access: Records accessed by key value
  • Automatic sorting: Records maintained in key sequence
  • Random or sequential: Supports both access modes
  • Updates and deletes: Can modify and remove records
  • Unique or duplicate keys: Supports both key types
  • Alternate keys: Can define alternate indexes

Defining KSDS in COBOL

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
ENVIRONMENT 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:

  • ORGANIZATION IS INDEXED: Specifies KSDS organization
  • ACCESS MODE: RANDOM for direct access, SEQUENTIAL for sequential access
  • RECORD KEY: The field used for indexing (must be in record structure)
  • Set key before READ: For RANDOM access, set RECORD KEY before reading

KSDS Sequential Access

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
SELECT 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 (Entry Sequenced Data Set)

ESDS stores records in the order they are written, similar to sequential files:

ESDS Characteristics

  • Write order: Records stored in order written
  • Sequential access only: Cannot access records randomly
  • Append-only: Cannot update or delete existing records
  • No keys: No indexing or key-based access
  • Useful for logs: Ideal for audit trails and log files

Defining ESDS in COBOL

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
ENVIRONMENT 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 (Relative Record Data Set)

RRDS provides direct access by relative record number (position):

RRDS Characteristics

  • Position-based access: Access records by relative record number (1, 2, 3, etc.)
  • Fixed-length slots: Each record has a fixed position
  • Random access: Direct access by record number
  • Updates and deletes: Can modify and remove records
  • Fixed record length: All records must be the same length

Defining RRDS in COBOL

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
ENVIRONMENT 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 File Operations

VSAM files use standard COBOL I/O statements with some VSAM-specific considerations:

Opening VSAM Files

cobol
1
2
3
4
5
6
7
8
9
10
11
*> 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

Reading VSAM Files

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
*> 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

Writing VSAM Files

cobol
1
2
3
4
5
6
7
8
9
10
11
12
*> 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

Updating VSAM Records

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
*> 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

Deleting VSAM Records

cobol
1
2
3
4
5
6
7
8
*> 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

Comparing VSAM Organization Types

VSAM Organization Type Comparison
AspectKSDSESDSRRDS
Access methodBy key (indexed)Sequential onlyBy relative record number
Record orderSorted by keyWrite orderBy position
Random accessYes (by key)NoYes (by position)
Sequential accessYes (in key order)Yes (in write order)Yes (by position)
UpdatesYes (REWRITE)NoYes (REWRITE)
DeletesYes (DELETE)NoYes (DELETE)
Keys requiredYes (RECORD KEY)NoNo (uses position)
Best forKey-based lookups, updatesLogs, audit trailsFixed-size lookup tables

Best Practices for VSAM

Follow these best practices:

  • Choose appropriate organization: Use KSDS for key-based access, ESDS for logs, RRDS for position-based access
  • Handle file status: Always check FILE STATUS after file operations
  • Use INVALID KEY: Handle INVALID KEY for random access operations
  • Close files properly: Always close VSAM files when done
  • Validate keys: Ensure key values are valid before random access
  • Handle duplicates: Understand duplicate key behavior if allowed
  • Use appropriate access mode: RANDOM for direct access, SEQUENTIAL for processing all records
  • Test error conditions: Test with missing records, invalid keys, and file errors
  • Document file structure: Document key fields, record structure, and organization type
  • Consider performance: KSDS indexes provide fast access but require maintenance

Common VSAM Patterns

Pattern 1: Key-Based Lookup

cobol
1
2
3
4
5
6
7
8
9
*> 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

Pattern 2: Sequential Processing

cobol
1
2
3
4
5
6
7
8
9
10
*> 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

Pattern 3: Update Record

cobol
1
2
3
4
5
6
7
8
9
10
11
12
*> 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

Explain Like I'm 5: VSAM

Think of VSAM files like different types of filing systems:

  • KSDS is like a filing cabinet with an index - you look up a name (key) in the index to find the file, and files are kept in alphabetical order
  • ESDS is like a diary - you write entries in order, and you read them in the same order you wrote them, but you can't go back and change old entries
  • RRDS is like a row of numbered boxes - you know box 5 is in position 5, box 10 is in position 10, and you can go directly to any box by its number

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)!

Practice Exercises

Complete these exercises to reinforce your understanding:

Exercise 1: KSDS Lookup

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.

Exercise 2: ESDS Logging

Create a program that writes log entries to an ESDS file, then reads them back sequentially and displays all log messages.

Exercise 3: RRDS Lookup Table

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.

Exercise 4: Sequential Processing

Create a program that processes all records in a KSDS file sequentially, calculating totals or performing operations on each record.

Exercise 5: Update Operations

Create a program that reads a KSDS record, modifies specific fields, and uses REWRITE to update the record. Handle errors appropriately.

Test Your Knowledge

1. What does VSAM stand for?

  • Virtual Sequential Access Method
  • Virtual Storage Access Method
  • Variable Storage Access Method
  • Virtual System Access Method

2. Which VSAM organization provides indexed access by key?

  • ESDS
  • KSDS
  • RRDS
  • PSDS

3. Which VSAM organization stores records in write order?

  • KSDS
  • ESDS
  • RRDS
  • All of them

4. Which VSAM organization provides access by relative record number?

  • KSDS
  • ESDS
  • RRDS
  • None of them

5. What access mode is used for random access to KSDS?

  • SEQUENTIAL
  • RANDOM
  • DYNAMIC
  • INDEXED

6. What must you set before reading a KSDS record with RANDOM access?

  • File status
  • Record key
  • Relative record number
  • File pointer

Related Concepts

Related Pages