Mainframes 360
The one stop destination for System Z professionals

Tuesday, March 16, 2010

Random Processing of VSAM Files

Q. What are the Environment Division considerations for random processing of KSDS File?
When you want to do random processing of the records in a KSDS File, the COBOL SELECT statement has the following syntax :

SELECT filename
   ASSIGN TO ddname
   ORGANIZATION IS INDEXED
   ACCESS MODE IS RANDOM
   RECORD KEY IS name-of-key-field-in-record
   FILE STATUS IS file-status-area

Image163[1] 

The Key field that is used to uniquely identify each employee is the Employee Identification no. – EMP-ID field.
Q. How Random retrieval of KSDS Files works? 
For the sake of example, let’s assume that we have a KSDS File containing Employee records. The contents of the KSDS file are -

Image164[1]

The record-layout for the Employees KSDS file is as shown below -

Image165[1]

For random retrieval of records, you just spell out the key-value(Employee identification no.) of the employee you wanna find, to VSAM and in a jiffy, VSAM fetches the Employee details for you. Now, let’s look at what happens in the background.

The key value(employee-id) of the Employee record, you want to fetch is first placed in the key-field area(EMP-ID). Let’s assume you want to retrieve the details – name, age, salary and joining date of the Employee No. 70.

VSAM compares the RECORD KEY value(Employee-id) specified in the program, to the entries in the root index set record. The first entry in the root index record >= RECORD KEY value, is used to point to the next lower level index set record. The entries in this index record is then compared to the RECORD KEY. Again the first entry >= RECORD KEY value is found, and points to a next lower level index set record.

This search process continues downward through the index levels, until an index entry points to a sequence set record. The first sequence set entry which is greater than or equal to the (RECORD KEY)Employee No. 70, points to the Control Interval(CI) in the KSDS Data file. This Control Interval(CI) must hold the record(if it is in the file). The CI is now retrieved and searched for the desired logical record.
Q. What are the PROCEDURE DIVISION statements for random retrieval of records in KSDS file.
You are going to write a COBOL Program to search the details of Employee No. 70 in the Employee KSDS File. The COBOL Program can be broken down into three paragraphs – open, fetch, close.

The 1000-MAIN-PARA performs 3 steps -

Image166[1]

Line 51 performs 2000-OPEN-FILE-PARA, which would open the KSDS File for reading the records.

Lines 52-55 are executed, only if the Open operation on the KSDS File is successful. If the Open operation fails, a message stating ERROR OPENING VSAM FILE is displayed, and the error code is printed to the log.

If the OPEN operation is successful, you go ahead and fetch the logical-record you want, and then close the files, once you are done.

The 2000-OPEN-FILE paragraph OPENs the EMPLOYEE-KSDS-FILE containing Employee records in INPUT mode. The INPUT mode is used to read the records from a file. It also OPENs the OUTPUT-FILE in OUTPUT mode. The OUTPUT-FILE points to the log(print output) of the job, and we shall print the details of the Employee we want, to the OUTPUT-FILE.

Image167[1]

The 3000-FETCH-RECORD paragraph will retrieve(search) the details of Employee-ID 70.

We begin by placing the key value(employee-id) of the employee, we would like to search in the RECORD KEY field – EMP-ID. Lines 68 places the value '070' in the Key-field EMP-ID

Image168[1]

On Line 69, the COBOL READ Statement, causes VSAM to perform a random search on the EMPLOYEE-KSDS-FILE for the record with EMP-ID 70. If the search returns positive results - Employee record with EMP-ID=70 is found, the
NOT INVALID KEY clause is executed, and a message ‘RECORD FOUND’ is printed to the log. If the search is a failure – there is no Employee record in the KSDS File with EMP-ID=70, the INVALID KEY clause is executed, and a message ‘RECORD NOT FOUND’ is printed to the log.

We then store the data present in the EMPLOYEE-KSDS-RECORD area into
OUTPUT-RECORD area. Line 77 write the OUTPUT-RECORD, details of the Employee no. 70 to the OUTPUT-FILE.

The 4000-CLOSE-PARA closes the files, and releases them.

Image169[1]

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.