Mainframes 360
The one stop destination for System Z professionals

Sunday, March 7, 2010

Sequential Processing of VSAM Files

Q. What are the ENVIRONMENT DIVISION considerations for sequential processing of KSDS File?
The SELECT statement in COBOL assigns a Symbolic-name(second name), to the actual TSO-file. So, I could use the symbolic file-name OUTPUT-FILE in COBOL, to refer to my file AGY0157.EMPLOYEE.KSDS.

SELECT Statement describes the file - (i) what’s the file-layout, the organization (ii) and the mode of processing(how are you going to process it).

File organization - Is the file sequential or does it also have an index, is it indexed. You also want to tell, how the records should be processed in a file – do you traverse the records one-by-one sequentially, and process the file from start-to-end, till you find the desired record – that’s sequential processing. On the other hand, you can directly jump(hop) or fly over-to the desired record(that’s a lot quicker) and process it – that’s random processing. For sequential processing of records, you must set the ACCESS MODE to Sequential.

The SELECT statement highlights the RECORD KEY. The value in record key field provides a unique identification for each record in the file. Every record will have a unique value in record key field. For example, in the Employees KSDS File, the record key is EMP-ID. This is because, every employee has a unique EMP-ID. No two employees have same EMP-ID.

First up, in the picture below, see what the SELECT statement for KSDS file has - 

Image158[1]  

Now, the File descriptor and record-layout(the various fields in Employee record) for the Employees KSDS file is shown in the picture :

Image157[1] 
Q. How do you perform initial loading of records in KSDS file?
When you create an empty KSDS file, you want to store some meaningful data in the file. Storing records, or populating an empty KSDS file with records is called Initial Load. You write records out one-by-one sequentially to the KSDS file. Thus, for the Initial load, the access mode is set to Sequential. During Initial Load, the records must be loaded in increasing(ascending) sequence of the RECORD KEY.

We are going to load records sequentially in the Employee KSDS File. To begin with, here are the Working storage section variables -

Image159[1]

Lines 42-44 define the END-OF-INPUT-FILE flag variable. This variable behaves like a switch, initially it is in NOT-AT-END state. When you have read all the records in the file, it flips over to AT-END state.

Lines 46-48 define the STATUS CODE area for the KSDS File. Whenever you perform a VSAM operation, like opening the file, writing to the file, or closing the file, it leaves behind a 2-digit code number. 00 indicates a successful operation and non-zero code indicates failure.

Make it a habit to use File Status codes because, trapping errors, and nipping them in the bud, helps your program to recover from the dangerous run-time errors, which could crash your program and leave your files or database in an inconsistent state.

Lines 50-51 define the counter variable which keeps track of the number of input records processed.

The procedure for sequentially loading a KSDS file, is as follows
1. OPEN OUTPUT filename
2. WRITE record-name
   FROM data-area
   INVALID KEY what to do
3. CLOSE filename

The WRITE statement in COBOL is used to write a record to a KSDS file. When you initially load a KSDS file, the RECORD KEY must be increasing(ascending) order. What happens when this condition is not met? The WRITE is aborted, and a STATUS CODE is set. The INVALID KEY clause is executed, when the WRITE operation fails. Thus, key-violation is automatically detected by the INVALID KEY clause. Thus, INVALID KEY Routine can be used to trap duplicate key, or out-of-sequence record errors. 

The picture below shows the PROCEDURE DIVISION statements -

Image160[1]

Line 55 performs the OPEN-FILE routine, which opens the KSDS file.

The conditional IF block(Lines 56-63) tests the value of FILE-STATUS-CODES variable. If the Open operation leaves behind a STATUS CODE 00(VSAM-FILE-OPERATION-OK), a loop is generated to load the records in the KSDS file and then close it. If a non-zero status code is left behind(ELSE block), you print a message to the log which says 'Unable to open indexed file'.

Lines 66-69 is the OPEN-FILE routine. EMPLOYEE-KSDS-FILE is opened for output on line 68.

Image161[1]

Lines 71-81 is the load a record into the Employees file. Line 72 reads a record from the input data. The record is placed in the INPUT-RECORD area. Once the record has been read, the counter WS-EMLOYEE-CNT is incremented. The WRITE statement in line 77 attempts to add this record to the file. WRITE automatically sequence checks the records on the RECORD KEY field. If WRITE operation fails, it is because of duplicate key, or out-of-sequence error. A message is DISPLAYed on line 80.

Here, I have just assumed that the WRITE operation fails because of key-violation. However, this may not always be the case(e.g. if the file fills up and no more space is available, or hardware I/O error). Ideally speaking, the STATUS CODE must be tested to differentiate between possible errors.

Image162[1]
Lines 83-92 CLOSE the KSDS file, after the loading is complete. The CLOSE operation leaves behind a STATUS CODE. If the CLOSE is successful, a message is DISPLAYed 'VSAM FILE CREATION COMPLETED'. The number of records processed are also displayed. If the CLOSE fails, the message 'VSAM FILE NOT PROPERLY CLOSED'.

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.