REWRITE and DELETE
A master file is not write-once. Customers move, balances change, accounts
close. Open the file I-O and you get three verbs besides READ:
| Verb | Does | Fails with |
|---|---|---|
WRITE |
Adds a new record | 22 key already exists |
REWRITE |
Replaces the record with the same key | 23 no such key (random/dynamic) |
DELETE file RECORD |
Removes the record with that key | 23 no such key |
Read, change, rewrite
To change one field you must first READ the record (so the rest of it is in the record area), change the field, then REWRITE:
IDENTIFICATION DIVISION.
PROGRAM-ID. PARTMNT.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT PART-FILE ASSIGN TO PARTMAST
ORGANIZATION IS INDEXED
ACCESS MODE IS DYNAMIC
RECORD KEY IS PART-NO
FILE STATUS IS WS-FS.
DATA DIVISION.
FILE SECTION.
FD PART-FILE.
01 PART-REC.
05 PART-NO PIC X(4).
05 PART-QTY PIC 9(4).
WORKING-STORAGE SECTION.
01 WS-FS PIC XX.
PROCEDURE DIVISION.
OPEN OUTPUT PART-FILE
MOVE "A1000050" TO PART-REC
WRITE PART-REC
MOVE "B2000007" TO PART-REC
WRITE PART-REC
CLOSE PART-FILE
OPEN I-O PART-FILE
MOVE "A100" TO PART-NO
READ PART-FILE
NOT INVALID KEY
ADD 25 TO PART-QTY
REWRITE PART-REC
DISPLAY "A100 NOW " PART-QTY
END-READ
MOVE "B200" TO PART-NO
DELETE PART-FILE RECORD
NOT INVALID KEY DISPLAY "B200 DELETED"
END-DELETE
MOVE "B200" TO PART-NO
DELETE PART-FILE RECORD
INVALID KEY DISPLAY "B200 AGAIN: STATUS " WS-FS
END-DELETE
CLOSE PART-FILE
STOP RUN.
Output:
A100 NOW 0075
B200 DELETED
B200 AGAIN: STATUS 23
Notice that DELETE takes the file name (DELETE PART-FILE RECORD)
while WRITE and REWRITE take the record name. It is an old
inconsistency and a favourite interview question.
Rules that bite
- You cannot change the primary key with REWRITE. The key identifies which record to replace. To "rename" a key, WRITE a new record and DELETE the old one.
- In
ACCESS SEQUENTIAL, REWRITE and DELETE apply to the record you just read, so a successful READ must come first. If the key field changed in between you get status21. - In
RANDOMorDYNAMIC, DELETE needs only the key. No READ first. - Check the status every time. INVALID KEY covers
22/23; anything else, like a9xhardware or logic error, needs a FILE STATUS check.
Transaction-driven maintenance
Real maintenance programs read a file of transactions, each carrying an action code, and apply them one by one:
EVALUATE TR-CODE
WHEN "A" PERFORM ADD-ACCOUNT
WHEN "U" PERFORM UPDATE-ACCOUNT
WHEN "D" PERFORM DELETE-ACCOUNT
END-EVALUATE
Each paragraph moves the transaction key into the record key, issues one verb, and reports success or a rejection. The rejects go to an error report that someone in operations works through the next morning.
On the job
While your batch job has a KSDS open I-O, the online (CICS) region
usually can't update it: VSAM share options, and whether CICS has
the file open, decide who gets to write. That is why master-file
updates run in the overnight batch window, and why a batch job that
overruns into the morning is a phone call you don't want.
Your task
Apply the day's account maintenance to the account master in place.
DD TRANIN — line sequential, 36-byte transactions:
| Field | PIC | Notes |
|---|---|---|
TR-CODE |
X |
A add, U update, D delete |
TR-ID |
X(6) |
account number |
TR-NAME |
X(20) |
used by A only |
TR-AMOUNT |
9(7)V99 |
A: opening balance, U: amount to add |
DD ACCTMAST — indexed, key ACCT-ID: ACCT-ID X(6),
ACCT-NAME X(20), ACCT-BAL 9(7)V99. Open it I-O.
For each transaction, in file order:
- A → write a new record:
ADDED nnnnnn - U → read it, add
TR-AMOUNTto the balance, rewrite:UPDATED nnnnnn - D → delete it:
DELETED nnnnnn - any failure →
REJECTED c nnnnnn STATUS sswith the real file status
Then display MASTER AFTER UPDATE: and list the whole master in key
order: ACCT-ID, space, ACCT-NAME, space, balance edited
Z,ZZZ,ZZ9.99. The starter already has the reject paragraph and the
listing.