VSAM RRDS Replace (Update)

Replacing a record in an RRDS means updating the content of an existing record at a given relative record number (RRN) without changing its position. You do this by reading the record (READ at that RRN), modifying the data in your buffer, and then issuing REWRITE. REWRITE writes the buffer contents back to the same slot or RRN, replacing the previous record. So replace is an in-place update: the RRN stays the same, the content changes. In a fixed RRDS the new record must be the same length as the old one. In a variable RRDS the new record can be a different length as long as it is within the defined min–max. This page explains how replace works, why you must READ before REWRITE, the difference between fixed and variable RRDS for REWRITE, and how to do it in COBOL.

What Is Replace (Update) in RRDS?

Replace means: change the content of the record at an RRN without moving it or freeing the RRN. You are not inserting a new record and you are not deleting. You are overwriting the existing record with new data. In VSAM the verb for this is REWRITE. REWRITE always applies to the "current" record—the one you last read. So the standard pattern is: position to the record (READ by RRN), change the data in memory, then REWRITE to write it back. The RRN does not change; the slot (fixed) or index entry (variable) still points to the same record location, but the content is updated.

READ Before REWRITE

You must successfully READ a record before you can REWRITE. REWRITE does not take an RRN as a parameter in the same way READ or WRITE do; it replaces "the record that was just read." So the sequence is: set the RRN (RELATIVE KEY), READ to load the record into your buffer, modify the buffer, then REWRITE. If you try to REWRITE without having read a record (or after a failed READ), the result is undefined and may cause an error or corrupt data. So replace is always a read-modify-write sequence.

Replace in Fixed-Length RRDS

In a fixed-length RRDS every record is the same length. When you REWRITE, the record you write back must be the same length as the one you read. So you read an 80-byte record, change some bytes (or all of them), and REWRITE an 80-byte record. You cannot REWRITE with a longer or shorter record; the slot size is fixed. If your record description in the program matches the file (e.g. PIC X(80)), the length is naturally preserved when you REWRITE.

Replace in Variable-Length RRDS (VRRDS)

In a variable-length RRDS records can have different lengths within the defined range (e.g. 50 to 200 bytes). When you REWRITE, the new record can be a different length than the old one, as long as it is within the minimum and maximum. So you might read a 80-byte record, modify it and perhaps shorten or lengthen it (e.g. to 100 bytes), and REWRITE. The access method updates the record in place (or moves it internally if the new length does not fit in the same space) and the index still maps the same RRN to the record. The exact behavior for length changes may depend on the implementation; consult your documentation.

Step-by-Step: Replace Pattern

Steps to replace (update) a record in RRDS
StepDescription
1. Set RRNMove the RRN of the record you want to update into RELATIVE KEY.
2. READIssue READ. If successful, the record is in your buffer.
3. ModifyChange the data in the record buffer (same RRN, new content).
4. REWRITEIssue REWRITE. The record at that RRN is replaced with the buffer contents.

COBOL: READ and REWRITE

In COBOL you set the RELATIVE KEY to the RRN, READ the record, modify the record area, and then REWRITE. The REWRITE uses the same record area (and the same logical position established by the READ). Example: update the record at RRN 15.

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
SELECT RRDSFILE ASSIGN TO RRDSFILE ORGANIZATION IS RELATIVE ACCESS MODE IS RANDOM RELATIVE KEY IS WS-RRN FILE STATUS IS WS-STATUS. ... 01 WS-RRN PIC 9(8) COMP. 01 RRDSREC. 05 REC-ID PIC 9(4). 05 REC-DATA PIC X(76). ... MOVE 15 TO WS-RRN READ RRDSFILE INVALID KEY DISPLAY 'No record at RRN 15' NOT INVALID KEY *> Modify the record ADD 1 TO REC-ID MOVE 'Updated data' TO REC-DATA REWRITE RRDSREC INVALID KEY DISPLAY 'REWRITE failed' NOT INVALID KEY DISPLAY 'Record at RRN 15 updated' END-REWRITE END-READ

The READ loads the record at RRN 15 into RRDSREC. The program then changes REC-ID and REC-DATA and issues REWRITE RRDSREC. The record at RRN 15 is replaced with the new content. For fixed-length RRDS, the length of RRDSREC must not change; for variable-length, the length can vary within the file's defined range.

Replace vs Delete and Insert

If you DELETE a record and then WRITE a new record at the same RRN (with REUSE), you effectively replace the content. But that is two operations (DELETE and WRITE) and the RRN is briefly empty. For a simple in-place update, READ then REWRITE is the standard and more efficient approach. Use delete and insert when you actually need to free the RRN (e.g. to reassign it or to enforce a different lifecycle). Use replace when you are only changing the content of an existing record.

Key Takeaways

  • Replace in RRDS is READ at the RRN, modify the record in the buffer, then REWRITE.
  • REWRITE replaces the last record read; you must READ before REWRITE.
  • In fixed RRDS the record length must stay the same on REWRITE.
  • In variable RRDS the new record length can vary within the defined min–max.
  • Replace is in-place update; the RRN does not change.

Explain Like I'm Five

Imagine a row of numbered boxes. To "replace" what's in box 4, you look in box 4 (READ), take out the toy, change it or put in a different toy (modify), and put it back in the same box 4 (REWRITE). The box number doesn't change—box 4 is still box 4—but what's inside is new. You have to look first (READ) so the computer knows which box to put the new thing in. That's replace.

Test Your Knowledge

Test Your Knowledge

1. What must you do before REWRITE in an RRDS?

  • DELETE the old record
  • READ the record at that RRN
  • WRITE a new record
  • Open the file for output

2. In a fixed RRDS, can the record length change when you REWRITE?

  • Yes, any length
  • No; the record length must stay the same
  • Only in variable RRDS
  • Yes, but only shorter

3. What does REWRITE do?

  • Inserts a new record
  • Deletes the record
  • Replaces the last read record with the buffer contents
  • Appends to the file
Published
Updated
Read time4 min
AuthorMainframeMaster
Reviewed by MainframeMaster teamVerified: IBM z/OS 2.5 documentationSources: IBM DFSMS Access Method Services, z/OS VSAM documentationApplies to: z/OS 2.5