Mainframes 360
The one stop destination for System Z professionals

Saturday, April 3, 2010

Updating Records in VSAM File

Q. What are the COBOL Instructions for randomly updating a KSDS File?
The REWRITE statement in COBOL is used to change the records in a KSDS File. You have to merely supply new values for the record-fields, and the record in the KSDS file gets updated. How does the REWRITE statement achieve this magnificent feat?

The ingredients that make up a RE-WRITE statement are (i) REad followed by (ii)WRITE. When a REWRITE statement is executed, what happens is this. Initially, it REads the record(you intend to modify) from the file into a temporary scratch-pad area or rough-work area. You specify the record you intend to modify, by mentioning its key-value. The modifications(new values) that you supply, are applied to the record that’s brought into the scratch-pad area. The old-values are wiped out(erased off), and the new values take their place. It finally WRITEs the updated record back to the file.
Q. What is the procedure to update records in a VSAM File?
I wish to update the Records in the Employee-Master File which is a VSAM File, shown in the picture below.

Image342
The copy-book EMPMAST contains the Record-Format of the EMPLOYEE-MASTER-FILE, is shown in this snap.

Image343

1. Pick-up the Record : First of all, you need to tell, which record in this VSAM File is to be modified? What you do is, just MOVE the key of the record to be changed in RECORD KEY field. Say, if I would like update data about RAKESH, I would MOVE 55 to the Key-Field EMP-ID. And, then I do a READ on the EMPLOYEE-MASTER-FILE. Behind the scenes, what it does is, the record '55 RAKESH 35 1650000 20080217' is searched in the file, it is read in, and brought into the COBOL Storage Area EMPLOYEE-MASTER-RECORD.
 
MOVE 55 TO EMP-ID.
READ EMPLOYEE-MASTER-FILE

2. Make the Changes : Now, that the record-to-be-updated 055 RAKESH 1650000 20080217 is stored in EMP-ID, EMP-NAME, EMP-AGE, EMP-SALARY and EMP-JDATE Fields, you supply the new values for the fields that you want to change. Say, I want to increase Rakesh's salary to 17.5k, so I just change the EMP-SALARY Value. I MOVE 1750000 to EMP-SALARY. This makes my new record 055 RAKESH 1750000 20080217, leaving the rest fields undisturbed.

MOVE 1750000 TO EMP-SALARY

3. REWRITE Back to the File on top of the Old Record : I have now prepared, constructed the new record, which is to replace the Old Record off the VSAM File. What remains to be done is, to overlay this brand-new record on top of the old-one in the file. To write this new employee-record back into the VSAM File, I do a REWRITE EMPLOYEE-MASTER-RECORD.
Q. How do you code this in COBOL?
I have written a COBOL Program, to update the Records in a VSAM File. The Program Master File-Data based on the Transactions from the Transaction-File. Here is the Program Header.

Image346

A Transactions-File contains the List of Transactions that need to be updated in the Master-File. 

Image344 
The Copy-Book TRANSREC containing the File-Layout for Transactions File is shown below.

Image345 
I have defined the Employee Master File containing Employee Records, and the Transactions file containing Transactions as follows.

Image347

Working-Storage Areas :
FILE-STATUS-CODES COBOL Variable, stores the two-digit status code indicating success or failure of any File Operation such as OPEN, READ, REWRITE, CLOSE performed on the VSAM File.


I used a Switch END-OF-TRANSACTIONS-FILE that can toggle between YES or NO. While reading records from the Transaction File, I am gonna check this flag -  to make sure, its set to NO, and I have not reach the End of the The File. Once, I reach the End of the File, I turn the Flag to YES, which is indicator to stop any more processing of the File.

I have also used the Debug-Switch to control and turn the DISPLAY's in the COBOL Program on or off.  

Image348

The Procedure of updating records in Employee-Master File has been broken down into 3 Tasks – 1000-HOUSEKEEPING, 2000-PROCESS-DATA and 3000-CLEANUP.
   
Image349

On Line 94, the 1000-HOUSEKEEPING Paragraph OPENs the Employee Master File in I-O Mode. The I-O Mode is used to update data in a File(Read and then Write). The Transactions File is OPEN'ed in INPUT and the transactions are read from this file sequentially one-by-one.

Image350 
The task of Processing the Data is further broken down into two paragraphs -2400-READ-TRANS-REC that reads a Transaction record from the Transactions file and updating it in Master File 2800-UPDATE-MASTER.

Image351

The 2400-READ-TRANS-REC Paragraph attempts to READ one transaction from the Transactions File. When the READ is successful, the Data read from the Transactions-File will be stored in TRANSACTION-RECORD COBOL Storage Area.
One-by-one, after all the Transactions have been read, and you do one more extra-Read, the AT END Special Condition becomes true, and the Block-of-Instructions inside the AT END, Lines 125-126  will be executed. I set the AT-END-OF-FILE Flag to TRUE(Turn it On). This will stop any further processing. Moreover, the GO TO statement causes the control to directly jump to the 2800-UPDATE-MASTER EX-IT Para.

Image352

The 2800-UPDATE MASTER Paragraph performs the function of looking-up a Matching record in the Master File, and if found, updates the Salary-Field of the Employee. Take a look.

Image353

On the Line 127, the Employee-ID read from the Transactions-File(and stored in the 05-Level Field TRANS-EMP-ID) is MOVEd to the EMP-ID Variable which is the Key-Field for the Employee Master File.

Scenario 1 - EMP-ID=55
I perform a READ on the EMPLOYEE-MASTER File, with Employee-ID(say 55) stored in the Key-Field EMP-ID. This triggers off a search in the Master File for the Employee-id 55. If such an Employee if found, its fine. On Line 138, the New-Salary Value obtained from the Transaction-record, is updated in the Master-Record. The modified, updated new Master-record is written back to the Master-File using REWRITE Statement.

Scenario 2 - EMP-ID=40
But, what if the Employee that you want to update is not present in Master File? That’s what happens with Employee-id 40, observe that there's no guy matching in the Master File with EMP-ID=40. When this happens, the INVALID KEY Condition is raised, and the way I have handled and trapped this error, is I DISPLAY 'EMPLOYEE NOT FOUND IN MASTER' Message, followed by the employee-id. Watch out for the GO TO Statement. I need to avoid and skip off this record, and move on to the next record. To skip the remaining part-of-processing and move on to the next record, I coded a GO TO 2800-UPDATE-MASTER-EXIT.

The 3000-CLEANUP Paragraph closes the Employee Master File and Transactions File.
Image354

Upon running the Program, the Employee Master File the salary fields of the Employees with employee-id=55 and employee-id=65 are updated.

Image355

Here's the Debug-Trace, that shows the flow-of-execution of paragraphs. Take a look at the error-message for the Employee with employee-id=40.

Image356

To many people who are thrown to work at a mainframe computer on their first job, they feel lost. Mainframe people seem to speak a completely different language and that doesn't make life easy. What's more, the books and manuals are incredibly hard to comprehend.

"What on earth is a Mainframe?" is an absolute beginner's guide to mainframe computers. We'll introduce you to the hardware and peripherals. We'll talk about the operating system, the software installed on a mainframe. We'll also talk about the different people who work on a mainframe. In a nutshell, we'll de-mystify the mainframe.

Readers based in India, can buy the e-book for Rs. 50 only or the print book. International readers based in the US and other countries can click here to purchase the e-book.