Professional Documents
Culture Documents
Processing With VSAM Files: Please Use Speaker Notes For Additional Information!
Processing With VSAM Files: Please Use Speaker Notes For Additional Information!
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
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.
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.
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:
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.
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.