VSAM RRDS Insert

Inserting a record into an RRDS means adding a new record at a specific relative record number (RRN). You do this by setting the RRN (e.g. in the COBOL RELATIVE KEY) to the slot or position where you want the record and issuing a WRITE. In a fixed RRDS the record is placed in that slot if the slot is empty—or if the slot was previously freed by DELETE and the cluster has REUSE. In a variable RRDS (VRRDS) the WRITE assigns that RRN to the new record and the index is updated. This page explains how insert works in fixed and variable RRDS, the effect of empty slots and REUSE, what happens when you write to an occupied slot, and how to do it in COBOL.

What Is Insert in RRDS?

In RRDS there is no separate "INSERT" verb in the usual sense. You insert by writing a record at an RRN. So "insert" means: set the RRN to the position where you want the new record and issue WRITE. If that slot is empty (or has been freed by DELETE and REUSE is in effect), the record is placed there. If the slot already has a record, the WRITE typically fails with a duplicate-record or invalid-key condition. So the key idea is: insert = WRITE at an RRN where no record currently exists (or where the slot has been freed for reuse).

Insert in Fixed-Length RRDS

In a fixed-length RRDS the data component is divided into fixed-size slots. Each slot has an RRN (1, 2, 3, …). When you WRITE a record, you specify the RRN; the record is placed in that slot. For the WRITE to succeed, the slot must be empty. A slot is empty if no record has ever been written there, or if a record was deleted and the cluster was defined with REUSE (so the slot is available again). If the slot already contains a record, you are not "inserting"—you are trying to add a second record to the same slot, which is not allowed. To change the existing record, you READ it, modify it, and REWRITE (see Replace).

Insert in Variable-Length RRDS (VRRDS)

In a variable-length RRDS there are no fixed slots. When you WRITE at an RRN, you are assigning that RRN to the new record. The access method stores the record in the data component and updates the index to map that RRN to the record's location. If that RRN already has a record (from a previous WRITE), the new WRITE may fail—you cannot have two records with the same RRN. If the RRN was previously freed by DELETE and the cluster has REUSE, you can WRITE a new record that will be assigned that RRN. So in VRRDS, insert is also WRITE at an RRN, with the index tracking which RRNs are in use.

Empty Slots and REUSE

In a fixed RRDS, when you first create the file all slots are empty. You can WRITE at any RRN from 1 up to the maximum number of slots. Once a slot has a record, you cannot WRITE another record to that same slot unless you first DELETE the record. If the cluster is defined with REUSE, the DELETE frees the slot and you can then WRITE a new record at that same RRN. So REUSE is what allows "delete then insert again at the same RRN." Without REUSE, the slot remains reserved but empty and may not accept a new WRITE depending on the system.

Insert scenarios and outcomes
ScenarioResult
Empty slot (fixed RRDS)WRITE places the record in that slot. RRN is now occupied.
Reused slot (REUSE, fixed RRDS)After DELETE, WRITE at same RRN places new record in the slot.
Occupied slotWRITE usually fails (duplicate/invalid key). Use READ then REWRITE to update.
New RRN (variable RRDS)WRITE assigns that RRN to the record; index is updated.

COBOL: WRITE for Insert

In COBOL you define the file with ORGANIZATION IS RELATIVE and RELATIVE KEY. For random (direct) access you move the desired RRN to the RELATIVE KEY and then WRITE the record. The record description must match the record length (fixed) or be within the allowed length (variable). Example: insert a record at RRN 20.

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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 PIC X(80). ... MOVE 20 TO WS-RRN MOVE 'Your record data here...' TO RRDSREC WRITE RRDSREC INVALID KEY DISPLAY 'WRITE failed - slot may be occupied or invalid' NOT INVALID KEY DISPLAY 'Record written at RRN 20' END-WRITE

If the slot at RRN 20 is empty (or reused), the WRITE succeeds. If it is already occupied, INVALID KEY is typically raised. Your program should handle INVALID KEY to decide whether to try another RRN or report an error.

Sequential Insert

Some applications want to add records in order: first record at RRN 1, next at RRN 2, and so on. You can do this by maintaining a counter (e.g. next RRN to use) and doing a direct WRITE at that RRN each time. Alternatively, if your system supports sequential WRITE for RRDS, you open for sequential output and issue WRITE; the system may assign the next available RRN. The exact behavior depends on the implementation. For full control over which RRN gets the record, use direct (random) access and set the RRN before each WRITE.

Key Takeaways

  • Insert in RRDS is done by WRITE at the desired RRN (set RELATIVE KEY, then WRITE).
  • In fixed RRDS the slot must be empty or freed (with REUSE) for WRITE to succeed.
  • In variable RRDS the RRN is assigned to the new record and the index is updated.
  • Writing to an occupied slot (or already-used RRN) typically causes INVALID KEY; use READ then REWRITE to update.
  • REUSE allows a deleted slot/RRN to be reused for a new WRITE.

Explain Like I'm Five

Imagine numbered mailboxes. To "insert" a letter, you put it in a mailbox. You pick the number (RRN)—say 5. If mailbox 5 is empty, you can put your letter in. If someone already put a letter there, you can't put another one in that same box (you'd have to take the old letter out first—that's delete—and if the rule is REUSE, then you can put a new letter in box 5). Insert = put something in an empty (or reused) box by choosing its number.

Test Your Knowledge

Test Your Knowledge

1. How do you insert a record at RRN 10 in an RRDS?

  • READ then REWRITE
  • Set RELATIVE KEY to 10 and issue WRITE
  • Use ADD or INSERT command
  • Sequential WRITE only

2. With REUSE, when can you WRITE to RRN 5 again after deleting the record there?

  • Never
  • Only after closing and reopening
  • Immediately; the slot is available for a new WRITE at RRN 5
  • Only in variable RRDS

3. What should you do to change an existing record in an RRDS?

  • WRITE at the same RRN
  • READ then REWRITE
  • DELETE then WRITE
  • Use UPDATE command
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