VSAM Troubleshooting
VSAM file status 21 sequence error
VSAM file status 21: Sequence error — records written out of key sequence to a KSDS.
What it means
- Sequence error — records written out of key sequence to a KSDS.
- The key of the new record is lower than the previous one.
Common causes
- Input file not sorted by key.
- Key built incorrectly.
- Restart resumed at the wrong point.
How to diagnose
- Sort the input by key before the run.
- Verify key construction.
- Check sort stability for duplicate keys.
How to fix it
- Sort input by key.
- Fix key construction.
- Correct the restart point.
Example: Always check FILE STATUS after I/O
READ CUSTFILE NEXT RECORD
AT END MOVE 'Y' TO WS-EOF
NOT INVALID KEY CONTINUE
END-READ.
IF WS-FS NOT = '00'
DISPLAY 'File status: ' WS-FS
END-IF.
FILE STATUS after every I/O turns mystery failures into clear codes.
Interview tip
- Q: How would you troubleshoot a VSAM file status 21 sequence error in production?
- A: I would confirm the exact file status code, check the JCL DD and catalog, then apply the specific fix.
Prevention checklist
- Check FILE STATUS after every OPEN/READ/WRITE.
- Keep FD layouts in sync with cluster definitions.
