You are on page 1of 20

Processing with VSAM Files

Please use speaker notes for


additional information!
Organization is sequential
Access is sequential

This code means that the file is organized


without indexes and will be read sequentially
SELECT INDEXED-FILE
one record at a time. Note that since sequential
ASSIGN TO “A:\PAYROLL.DAT”
is the default for both organization and access,
ORGANIZATION IS SEQUENTIAL
these clauses do not need to be specified.
ACCESS IS SEQUENTIAL.

The organization of the tells how the data is organized. Organization sequential means that it is
organized without indexes. Organization indexed means that it is an indexed file with a primary
index and possible alternate indexes that can be used to access the records. Essentially that
means that there will be a data file and an index file used to access the data. The third type of
organization is relative which will be not be dealt with in this presentation.
Once a file has been set up with indexes, other programs reading or updating this file must also
specify the indexes. The indexes can then be used to access the records on the file. Today
indexed files are VSAM (Virtual Storage Access Method).
Access refers to how the records on the file will be accessed. The access method is directly
related to the organization. For example if the organization is sequential, the records can only be
accessed sequentially.
Organization is indexed
Access is sequential

This file is an indexed VSAM file. That means


SELECT INDEXED-FILE
that the records are stored on a disk using a
ASSIGN TO “A:\PAYROLL.DAT”
methodology that includes indexes. This means
ORGANIZATION IS INDEXED
that there will be two files: one containing the
ACCESS IS SEQUENTIAL
data and one containing the indexes. The index
RECORD KEY IS IDNO.
can be either numeric or alphanumeric. The
index must be unique so frequently a field like
FD INDEXED-FILE
social security number or some other
DATA RECORD IS INDEXED-REC.
identification number is used as the index.
01 INDEXED-REC.
05 IDNO PIC 9(5). In this example, the file is organized so that it
05 ... has indexes. The field that is the index is
specified in the record key clause. The access
on this select is sequential which means that the
file will be read sequentially one record at a
time. Note that the indexed field that is the
record key must then be defined in the FILE-
SECTION.
Organization is indexed
Access is random

SELECT INDEXED-FILE With random access, the indexed structure can


ASSIGN TO “A:\PAYROLL.DAT” be used to access a specific record based on the
ORGANIZATION IS INDEXED record key.
ACCESS IS RANDOM
The structure of the program usually involves
RECORD KEY IS IDNO.
reading a transaction that contains a particular
identification number and then randomly
FD INDEXED-FILE
finding a match to that identification number in
DATA RECORD IS INDEXED-REC.
the indexed file. (Note I used identification
01 INDEXED-REC.
number because that is the record key in the
05 IDNO PIC 9(5).
example).
05 ...
Organization is indexed
Access is dynamic

With indexed organization, you can also


specify that ACCESS IS DYNAMIC. This
SELECT INDEXED-FILE
means that the indexed file can be read both
ASSIGN TO “A:\PAYROLL.DAT”
sequentially and dynamically in this
ORGANIZATION IS INDEXED
program. For example, you might
ACCESS IS DYNAMIC
randomly access a particular record and
RECORD KEY IS IDNO.
then process the records after it in the file
sequentially until a particular condition is
FD INDEXED-FILE
met. As we will see, with dynamic access
DATA RECORD IS INDEXED-REC.
the programmer must write code to specify
01 INDEXED-REC.
whether the read is random or sequential.
05 IDNO PIC 9(5).
05 ...
Alternate key SELECT INDEXED-FILE
ASSIGN TO “A:\PAYROLL.DAT”
ORGANIZATION IS INDEXED
ACCESS IS RANDOM
RECORD KEY IS IDNO
ALTERNATE RECORD KEY IS EMPNAME
SELECT INDEXED-FILE WITH DUPLICAT3S.
ASSIGN TO “A:\PAYROLL.DAT”
ORGANIZATION IS INDEXED FD INDEXED-FILE
ACCESS IS SEQUENTIAL DATA RECORD IS INDEXED-REC.
RECORD KEY IS IDNO 01 INDEXED-REC.
ALTERNATE RECORD KEY IS EMPNAME 05 IDNO PIC 9(5).
WITH DUPLICATES. 05 EMPNAME PIC X(20).
FD INDEXED-FILE
DATA RECORD IS INDEXED-REC. SELECT INDEXED-FILE
01 INDEXED-REC. ASSIGN TO “A:\PAYROLL.DAT”
05 IDNO PIC 9(5). ORGANIZATION IS INDEXED
05 EMPNAME PIC X(20). ACCESS IS DYNAMIC
RECORD KEY IS IDNO
ALTERNATE RECORD KEY IS EMPNAME
WITH DUPLICATES.

FD INDEXED-FILE
DATA RECORD IS INDEXED-REC.
01 INDEXED-REC.
05 IDNO PIC 9(5).
05 EMPNAME PIC X(20).
Creating a VSAM file SELECT INDEXED-FILE
ASSIGN TO “A:\PAYROLL.DAT”
ORGANIZATION IS INDEXED
ACCESS IS SEQUENTIAL
RECORD KEY IS IDNO.

FD INDEXED-FILE
DATA RECORD IS INDEXED-REC.
01 INDEXED-REC.
05 IDNO PIC 9(5).
05 ...
WRITE INDEXED-REC
INVALID KEY
PERFORM B-300-ERROR-WRITE.

In this example, I am going to be creating


a indexed sequential file with IDNO as
the key. When dealing with indexes there
is the possibility that I will encounter a
situation where the write is invalid. In
these cases, the invalid key clause on the
write will be triggered. If the write is
invalid, the write will not be executed.
Instead processing will go to B-300-
ERROR-WRITE.
Write an indexed file SELECT INDEXED-FILE
ASSIGN TO “A:\PAYROLL.DAT”
ORGANIZATION IS INDEXED
ACCESS IS SEQUENTIAL
RECORD KEY IS IDNO.

FD INDEXED-FILE
DATA RECORD IS INDEXED-REC.
01 INDEXED-REC.
05 IDNO PIC 9(5).
05 ...
WRITE INDEXED-REC
INVALID KEY
PERFORM B-300-ERROR-WRITE
NOT INVALID KEY There is also a NOT INVALID KEY
PERFORM B-310-PAPER-TRAIL clause that allows the programmer to
END-WRITE specify activity that is to take place if
the WRITE was successful. For
example, the programmer could have a
routine that writes a report containing
all of the records successfully written tot
he file.
Note that the END-WRITE clause can
be used with the WRITE as well.
Creating an indexed file
B-200-LOOP.
SELECT SEQ-PAY-FILE MOVE ID-IN TO IDNO.
ASSIGN TO “A:\PAYORIG.DAT” MOVE REST-IN TO REST-DATA.
ORGANIZATION IS SEQUENTIAL WRITE INDEXED-REC
ACCESS IS SEQUENTIAL. INVALID KEY
SELECT INDEXED-FILE PERFORM B-300-WRITE-ERR.
ASSIGN TO “A:\PAYROLL.DAT” READ PAYORIG
ORGANIZATION IS INDEXED AT END
ACCESS IS SEQUENTIAL MOVE “NO “ TO MORE-RECS.
RECORD KEY IS IDNO.
DATA DIVISION.
FILE SECTION.
FD PAYORIG
DATA RECORD IS PAYIN-REC.
01 PAYIN-REC.
05 ID-IN PIC 9(5).
05 REST-IN PIX X(100).
FD INDEXED-FILE
DATA RECORD IS INDEXED-REC.
01 INDEXED-REC.
05 IDNO PIC 9(5).
05 REST-DATA PIC X(100).
Reading an indexed file sequentially SELECT INDEXED-FILE
ASSIGN TO “A:\PAYROLL.DAT”
ORGANIZATION IS INDEXED
ACCESS IS SEQUENTIAL
RECORD KEY IS IDNO.

FD INDEXED-FILE
DATA RECORD IS INDEXED-REC.
01 INDEXED-REC.
05 IDNO PIC 9(5).
05 ...
READ INDEXED-FILE
AT END
MOVE “NO “ TO MORE-RECS.

When an indexed file is being read sequentially the


usual sequential read is used. Essentially, you can
read the indexed file sequentially from record one
until end of file has been reached. The processing
will be the same as if the organization was
sequential.
START verb EQUAL TO
=
GREATER THAN
START filename KEY IS >
NOT LESS THAN
NOT <
GREATER THAN OR EQUAL TO
>=
INVALID KEY… In this example, I have the user
NOT INVALID KEY… enter an identification number
END-START which is accepted to an area in
working storage. The entered
DISPLAY “ENTER START IDNO”. number is then moved to IDNO
ACCEPT IDNO-ENTERED. which is the RECORD KEY. I
MOVE IDNO-ENTERED TO IDNO then look for a starting point
START INDEXED-FILE based on the KEY >= to IDNO.
KEY >= IDNO If the user entered 45678 then
INVALID KEY processing would start with
DISPLAY “INVALID START POINT” either 45678 or the first record
MOVE “NO “ TO FOUND-IND larger than 45678. If no record
NOT INVALID KEY was equal or greater than other
MOVE “YES” TO FOUND-IND processing needs to be coded.
END-START
IF FOUND-IND = “YES” NOTE: The START positions, a
READ INDEXED-FILE READ is needed to actually read
AT END the record.
MOVE “NO “ TO MORE-RECS.
Sequentially reading using alternate key SELECT INDEXED-FILE
ASSIGN TO “A:\PAYROLL.DAT”
ORGANIZATION IS INDEXED
ACCESS IS SEQUENTIAL
RECORD KEY IS IDNO
When you want to read sequentially ALTERNATE RECORD KEY IS EMPNAME
along the alternate key path, you use WITH DUPLICATES.
the START verb to establish both the FD INDEXED-FILE
alternate key path and the starting DATA RECORD IS INDEXED-REC.
point. In this example, I want to read 01 INDEXED-REC.
the starting with the first record along 05 IDNO PIC 9(5).
the alternate key path. To do this, I 05 EMPNAME PIC X(20).
move LOW-VALUES to the field
defined as the ALTERNATE RECORD
KEY and then in the KEY clause of the
START, I again use the name of the
ALTERNATE RECORD KEY. This
not only locates the record but also
establishes the path to be followed with MOVE LOW-VALUES TO EMPNAME.
the sequential READ. START INDEXED-FILE
KEY GREATER THAN EMPNAME
INVALID KEY
...
READ INDEXED-FILE
AT END
MOVE “NO “ TO MORE-RECS.
Random read - record/prime key SELECT INDEXED-FILE
ASSIGN TO “A:\PAYROLL.DAT”
ORGANIZATION IS INDEXED
ACCESS IS RANDOM
RECORD KEY IS IDNO.

MOVE TRAN-ID TO IDNO. FD INDEXED-FILE


READ INDEXED-FILE DATA RECORD IS INDEXED-REC.
INVALID KEY 01 INDEXED-REC.
PERFORM B-310-PROBLEM 05 IDNO PIC 9(5).
NOT INVALID KEY 05 ...
PERFORM B-300-PROCESS
END-READ
MOVE TRAN-ID TO
IDNO establishes the key.
The READ that randomly
OR reads the file looking for a
record with that IDNO.
MOVE TRAN-ID TO IDNO. If the read is not successful
READ INDEXED-FILE in finding a record with that
INVALID KEY IDNO, the INVALID KEY
PERFORM B-310-PROBLEM. clause is executed.
Random read - alternate key SELECT INDEXED-FILE
ASSIGN TO “A:\PAYROLL.DAT”
ORGANIZATION IS INDEXED
ACCESS IS RANDOM
RECORD KEY IS IDNO
MOVE EMP-NAME-TRAN TO EMPNAME. ALTERNATE RECORD KEY IS EMPNAME
READ INDEXED-FILE WITH DUPLICAT3S.
KEY IS EMPNAME
INVALID KEY FD INDEXED-FILE
PERFORM B-310-PROBLEM DATA RECORD IS INDEXED-REC.
NOT INVALID KEY 01 INDEXED-REC.
PERFORM B-300-PROCESS 05 IDNO PIC 9(5).
END-READ 05 EMPNAME PIC X(20).

OR
The employee name from the transaction is
move to the EMPNAME to establish the
MOVE EMP-NAME-TRAN TO EMPNAME.
key. Since this READ is going to be using
READ INDEXED FILE
the ALTERNATE RECORD KEY the KEY
KEY IS EMPNAME
clause has to be used in the READ to
INVALID KEY
establish the fact that we are searching for a
PERFORM B-310-PROBLEM.
match in the alternate index, not the prime
index.
Read sequentially SELECT MASTER-FILE
&/or randomly ASSIGN TO "C:\PCOBWIN\VSAM\VSAMALT.DAT"
ORGANIZATION IS INDEXED
ACCESS IS DYNAMIC
RECORD KEY IS MID
ALTERNATE RECORD KEY IS MITEM-NAME WITH DUPLICATES.

This file is organized with indexes and the ACCESS IS DYNAMIC clause allows the programmer
to access the file either sequentially or randomly using either the record key MID or the alternate
record key MITEM-NAME.

Sequential read using primary key: Random read using primary key:

(Assumes starting point has been


MOVE RETR-ID TO MID.
established).
READ MASTER-FILE
INVALID KEY
READ MASTER-FILE NEXT
PERFORM B-310-INVALID
AT END
NOT INVALID KEY
MOVE "YES" TO EOF-IND
PERFORM B-300-PROCESS
END-READ.
END-READ.

When reading a ACCESS IS DYNAMIC file


sequentially, the NEXT is required to
differentiate between the two types of READs.
Random and MOVE START-PT TO MID.
sequential reads MOVE "YES" TO MSTR-FOUND.
READ MASTER-FILE
INVALID KEY
MOVE "NO " TO MSTR-FOUND.
IF MSTR-FOUND = "YES"
PERFORM B-300-DETAIL
EOF-MSTR = "YES”.

B-300-DETAIL. This code shows the START-PT being moved


...processing... to the MID (the record key) and then a random
READ MASTER-FILE NEXT read being done to locate the record that is
AT END requested. When the record is found, control
MOVE "YES" TO EOF-MSTR. will pass to the B-300-DETAIL.
In this routine, the file will be read
sequentially from the record located by the
random read. Note that the sequential read
uses the NEXT. When EOF is reached,
processing will end.
Start and sequential read
Random and sequential read

In this code the start verb locates the starting


In this code a random read locates the starting record but does not read it. The first time the
record. If the locate is successful, then the READ is executed the record that was located
NEXT record is read and processed. is read and processed.

MOVE START-PT TO MID. MOVE START-PT TO MID.


MOVE "YES" TO MSTR-FOUND. MOVE “YES” TO MSTR-FOUND.
READ MASTER-FILE START MASTER-FILE
INVALID KEY KEY >= MID
MOVE "NO " TO MSTR-FOUND INVALID KEY
NOT INVALID KEY MOVE “NO “ TO MSTR-FOUND
MOVE “YES” TO MSTR-FOUND NOT INVALID KEY
END-READ. MOVE “YES” TO MSTR-FOUND
IF MSTR-FOUND = "YES” END-START
READ MASTER-FILE NEXT IF MSTR-FOUND = “YES”
AT END READ MASTER-FILE
MOVE "YES" TO EOF-MSTR. AT END
MOVE “YES’ TO EOF-MSTR.
To mix random and sequential read
statements, dynamic access must be
specified.
Alternate key SELECT MASTER-FILE
ASSIGN TO "C:\PCOBWIN\VSAM\VSAMALT.DAT"
ORGANIZATION IS INDEXED
ACCESS IS DYNAMIC
RECORD KEY IS MID
ALTERNATE RECORD KEY IS MITEM-NAME WITH DUPLICATES.

Sequential read using alternate key: Random read using alternate key:

(Assumes that the start point and alternate key path MOVE RETR-NAME TO MITEM-NAME.
has been established). READ MASTER-FILE
KEY IS MITEM-NAME
READ MASTER-FILE NEXT INVALID KEY
AT END PERFORM B-410-INVALID
MOVE "YES" TO EOF-IND. NOT INVALID KEY
PERFORM B-400-PROCESS.

The sequential read is assuming that


the path has been established by a
previous random read.
File status SELECT MASTER-FILE
ASSIGN TO "C:\PCOBWIN\VSAM\VSAM1.DAT"
ORGANIZATION IS INDEXED
ACCESS IS RANDOM
RECORD KEY IS MID
FILE STATUS IS WS-FILE-STATUS.

01 WS-FILE-STATUS PIC XX VALUE SPACES.

MOVE RETR-ID TO MID.


READ MASTER-FILE
INVALID KEY
PERFORM B-310-INVALID.
IF WS-FILE-STATUS = "00"
PERFORM B-300-PROCESS.

Because of the file status clause in the B-310-INVALID.


select, the file status is retrieved every MOVE RETR-ID TO PID.
time this record is used. A file status of IF WS-FILE-STATUS = "23"
00 means that the processing was MOVE "RECORD MISSING" TO PMSG
successful. Charts that give meaning to ELSE
the specific numbers are available in
COBOL manuals. MOVE "OTHER PROBLEM" TO PMSG.
File status SELECT MASTER-FILE
ASSIGN TO "C:\PCOBWIN\VSAM\VSAM1.DAT"
01 WS-FILE-STATUS ORGANIZATION IS INDEXED
PIC XX ACCESS IS RANDOM
VALUE SPACES. RECORD KEY IS MID
FILE STATUS IS WS-FILE-STATUS.

B-200-LOOP.
PROCEDURE DIVISION. MOVE SPACES TO PRINTZ.
DECLARATIVES. MOVE RETR-ID TO MID.
ERROR-HANDLING SECTION. READ MASTER-FILE.
USE AFTER ERROR PROCEDURE IF WS-FILE-STATUS = "00"
ON MASTER-FILE. PERFORM B-300-PROCESS
ERROR-CHECK. END-IF.
IF WS-FILE-STATUS = "23"
MOVE RETR-ID TO PID
MOVE "RECORD MISSING " TO PMSG The declaratives part of the
WRITE PRINTZ program is where you can set up
AFTER ADVANCING 1 LINES error handling procedures that
END-IF. can be applied to a file. Note
END DECLARATIVES. that the read has no clause
MAIN-PROGRAM SECTION. associated with it. If the read is
MAINLINE. unsuccessful control switches to
PERFORM A-100-INITIALIZE. the the error-handling section
PERFORM B-100-PROCESS. and appropriate processing is
PERFORM C-100-TERMINATE. done.
STOP RUN.

You might also like