Sequential access in a VSAM Key Sequenced Data Set (KSDS) means reading records in key order, one after another. You do not supply a key for each read; instead, you establish a position (at the start of the file or at a key via START) and then use READ NEXT to get the next record in ascending key order, or READ PREV for descending key order where supported. Sequential access is used for batch jobs that process the whole file, for reports that list records in key order, and for processing a range of keys (e.g. all customers from CUST001 to CUST999). This page explains how sequential access works, how to use READ NEXT and START in COBOL, and how it differs from random and dynamic access.
In sequential access the records are read in the order they are stored. For a KSDS, that order is key order—ascending by primary key (or by alternate key if you use a path). So the "sequence" is the key sequence. Your program opens the file, optionally positions with START, then in a loop issues READ NEXT. Each READ returns the next record in key order and advances the position. When there are no more records, the READ fails with an "at end" condition (or equivalent file status). You never supply the key in the READ itself; the access method keeps track of the current position and returns the next logical record.
Sequential reading is efficient for full-file scans because VSAM reads control intervals in order. Once a CI is in the buffer, all records in that CI can be returned with READ NEXT without additional I/O until the next CI is needed. So sequential access tends to have good throughput when you are processing a large portion of the file or walking a range of keys.
READ NEXT retrieves the next record in ascending key order. After each successful READ NEXT, the position moves to that record, so the next READ NEXT returns the following record. READ PREV (when supported by your COBOL and VSAM level) retrieves the previous record in key order—useful for backward scans. For pure random access you use READ without NEXT: you set the key and read that one record; the position may or may not change depending on implementation. With ACCESS IS SEQUENTIAL you typically only use READ NEXT (and possibly START); with ACCESS IS DYNAMIC you can mix START, READ NEXT, READ PREV, and random READ.
| Read type | Effect |
|---|---|
| READ NEXT | Forward in key order; returns next record from current position |
| READ PREV | Backward in key order; returns previous record (when supported) |
| READ (no NEXT) | Random: use current key value to read that one record |
To use sequential access only, specify ACCESS IS SEQUENTIAL in the SELECT. The RECORD KEY is still required for a KSDS (it defines the key field), but you do not set it for each read; instead you use READ file-name NEXT. Example:
12345678910111213141516SELECT CUSTFILE ASSIGN TO CUSTFILE ORGANIZATION IS INDEXED ACCESS IS SEQUENTIAL RECORD KEY IS CUST-ID FILE STATUS IS WS-STATUS. ... OPEN INPUT CUSTFILE. PERFORM UNTIL exit READ CUSTFILE NEXT AT END SET exit TO TRUE NOT AT END PERFORM PROCESS-RECORD END-READ END-PERFORM. CLOSE CUSTFILE.
This reads every record from the first (lowest key) to the last (highest key). AT END is true when there are no more records. PROCESS-RECORD is executed for each record. For a large file this is the standard batch pattern.
Sometimes you do not want to start at the beginning of the file. You want to start at a specific key (or the first record greater than or equal to a key) and then read sequentially from there. The START statement sets the current position. You put the key value in the RECORD KEY field (or the alternate key field if using an alternate index), then issue START with a key condition. Common conditions: KEY IS EQUAL TO (position at that key, or first record with that key if duplicates), KEY IS GREATER THAN OR EQUAL TO (position at first record with key >= value). After a successful START, the next READ NEXT returns the record at that position (or the next one, depending on the condition). So you can process "all records from key X onward" by START KEY IS GREATER THAN OR EQUAL TO X, then loop READ NEXT until you pass the range you care about.
1234567891011121314151617MOVE 'CUST001000' TO CUST-ID. START CUSTFILE KEY IS GREATER THAN OR EQUAL TO CUST-ID INVALID KEY DISPLAY 'No records from this key' NOT INVALID KEY PERFORM UNTIL exit READ CUSTFILE NEXT AT END SET exit TO TRUE NOT AT END IF CUST-ID > 'CUST001999' SET exit TO TRUE ELSE PERFORM PROCESS-RECORD END-IF END-READ END-PERFORM END-START.
This positions at the first record with key >= CUST001000, then reads sequentially until the key exceeds CUST001999. So you process only the range of keys you need.
With ACCESS IS DYNAMIC you can use both random and sequential reads on the same file in the same program. For example: do a random READ by key to get one record, then later use START and READ NEXT to process a range. Or use START to position at a key, then READ NEXT in a loop. The key point is that after any READ (random or NEXT), the current position is updated, so the next READ NEXT continues from that position. This flexibility is useful when a program sometimes needs to look up one record and sometimes needs to walk a range.
Forward sequential is the usual: READ NEXT in ascending key order. Backward sequential uses READ PREV to read in descending key order. Not all COBOL/VSAM configurations support READ PREV; when they do, you typically use START to position at the end (e.g. KEY IS GREATER THAN last key) or at a high key, then READ PREV repeatedly. Use backward sequential when you need to process records in reverse key order (e.g. newest first).
When you issue READ NEXT and there is no next record (end of file), the read fails with an "at end" condition. In COBOL you handle this with AT END in the READ statement or by checking FILE STATUS (e.g. 10 for end of file). After at end, the position is undefined for the next READ NEXT unless you do another START. So your loop should exit when AT END is true and not attempt further READ NEXT without repositioning.
Use sequential access when you need to process records in key order: batch reports, full-file updates, range scans (START + READ NEXT), or any job that reads a large fraction of the file in order. Use random access when you need to fetch specific records by key (lookups). Use dynamic access when the same program does both—for example, read a control record by key, then sequentially process detail records in a range.
Imagine a row of boxes in order by number. Sequential access is walking along the row: you take the first box, then the next, then the next. You don't jump to box 50—you go 1, 2, 3, … until you finish. START is like saying "start from box 10"; then you take 10, 11, 12, … That's READ NEXT in key order.
1. Which COBOL verb reads the next record in key order?
2. What does START do in VSAM sequential processing?
3. Which access mode allows both sequential and random reads?