Sunteți pe pagina 1din 21

VSAM

On IBM mainframe systems, file processing is handled by special components of the


operating system called Access Methods.
VSAM which stands for Virtual Storage Access Method, was developed in Mid 1970s as a
replacement for less efficient access methods
Although VSAM is one access method, it supports for three types of files supported by
COBOL
These 3 types of files are referred as Sequential, Indexed and Relative Files under
COBOL
Under VSAM, they are referred as Entry-Sequenced, Key Sequenced & Relative record
datasets.Under VSAM terminology, a file or a dataset is referred as a Cluster
VSAM stores the logical records of a file in fixed length blocks called Control Intervals
A Group of Control Intervals in referred as Control Area


Logical Record 1 Logical Record 2 Logical Record 3 Logical Record 4
Logical Record 4 Logical Record 5 Logical Record 6 Logical Record 7
Logical Record 8 Logical Record 9 Logical Record 10 Logical Record 11
Logical Record 12 Logical Record 13 Logical Record 14 Logical Record 15
Control Interval 1
Control Interval 2
Control Interval 3
Control Interval 4
CONTROL AREA
VSAM
Linear Data Sets - A Linear Data Set(LDS) is a VSAM data set with a CI size of
4096 bytes & has no embedded control information in its CIs. So, all LDS bytes are data
bytes. An LDS has only a data component
Entry-Sequenced datasets - An Entry-sequenced dataset(ESDS) is the simplest type of
VSAM file. Its records are stored one after another in consecutive order. When the program
reads a record from an ESDS, VSAM gives it the record from the next physical location on
the disk. When it writes, VSAM writes it on the next physical location.
Key Sequenced Datasets - A Key Sequenced dataset (KSDS) is a set up so that you
can access its records sequentially or randomly, depending on the processing requirement.
To accomplish this, KSDS maintains an INDEX component that relates the value in the KEY
field to the actual location of the record on the disk. KSDS VSAM files are most widely used
VSAM files
Relative Record Datasets - A Relative Record dataset (RRDS) lets the programmer
access the record without the overhead of maintaining an index. Instead, each record in an
RRDS is numbered from 1. Then, to access a record in RRDS, one has to know the relative
record number. Usually, routines are built to convert a Key into Relative Record Number
Among the above, KSDS & ESDS are extensively used in all the commercial applications
VSAM - KSDS
A KSDS has a data and an index component. The index component keeps track of the used
keys and is used by VSAM to retrieve quickly a record from the data component, when a
request is made for a record with a certain key.
The index component of KSDS has two parts. A Sequence Set and an Index Set.
The highest level of index always has just one record. All the records of the lowest level of
the index set always contains pointers to the sequence set
The sequence set has pointers to the data records
KSDS will have free space reserved within its data component to accommodate new records.
This is achieved by either leaving space within each control interval or leaving entire control
intervals empty
The sequence set contains information that relates Key values to the control intervals in the
data component
For each control area in the data component there is one record in the sequence set
Each sequence set record contains an index entry to each control interval in corresponding
control area
The sequence set records also contain free pointers


INDEX RECORD
INDEX RECORD INDEX RECORD
INDEX RECORD INDEX RECORD INDEX RECORD INDEX RECORD INDEX RECORD INDEX RECORD
CONTROL AREA CONTROL AREA CONTROL AREA CONTROL AREA CONTROL AREA CONTROL AREA

CONTROL INTERVAL
CONTROL INTERVAL
CONTROL INTERVAL
CONTROL INTERVAL
CONTROL INTERVAL
CONTROL INTERVAL
CONTROL INTERVAL
CONTROL INTERVAL
CONTROL INTERVAL
CONTROL INTERVAL
CONTROL INTERVAL
CONTROL INTERVAL
CONTROL INTERVAL
CONTROL INTERVAL
CONTROL INTERVAL
CONTROL INTERVAL
CONTROL INTERVAL
CONTROL INTERVAL
CONTROL INTERVAL
CONTROL INTERVAL
CONTROL INTERVAL
CONTROL INTERVAL
CONTROL INTERVAL
CONTROL INTERVAL
CONTROL INTERVAL
CONTROL INTERVAL
CONTROL INTERVAL
CONTROL INTERVAL
CONTROL INTERVAL
CONTROL INTERVAL
CONTROL INTERVAL
CONTROL INTERVAL
CONTROL INTERVAL
CONTROL INTERVAL
CONTROL INTERVAL
CONTROL INTERVAL
D
A
T
A
I
N
D
E
X
----------------------INDEX SET ---------------------
SEQUENCE SET
COMPONENTS OF A KEY SEQUENCED DATA SET
VSAM KSDS
VSAM KSDS
012 049 187 EMPTY
188 210 247 EMPTY
301 315 369 EMPTY
FREE SPACE
012 411 419 EMPTY
480 501 512 EMPTY
516 517 518 EMPTY
FREE SPACE
699 518 369 EMPTY
FREE POINTER 369 247 187 FREE POINTER 518 512 419
CONTROL AREA 1 CONTROL AREA 2
D
A
T
A
SEQUENCE SET
INDEX SET
VSAM - KSDS
KSDS stores records in key sequence. When a record is added to KSDS, VSAM inserts the
record in its correct sequential location in the proper control interval and shift the records
that follow it.
If there is not enough space in the control interval to hold the inserted record, VSAM
performs a Control Interval split by moving some of the records into one of the free
control interval
If there is no free control interval in the control area, VSAM performs a control area split
by allocating a new control area, moving half of the control intervals from the original one to
the new control area
All indexed files have one primary index and one more alternate index. An alternate index
allows to access records by a field other than primary key
An alternate Index is a VSAM object which relates the alternate key with the primary key.
When accessing using an alternate index VSAM first matches the alternate key with
corresponding primary key and then uses the index component to retrieve the record.
Every time a record is added, updated or deleted form the data component, alternate index
is NOT updated. Alternate indexes are updated periodically in the nightly batch cycles.


VSAM - KSDS
Access Method Services - Access Method Services is a general-purpose utility program
that provides a variety of services for VSAM files. The AMS Commands can be executed thru
JCL utility IDCAMS. The following are some of the important AMS commands
ALTER - Change information specified for a catalogue, cluster,
alternate index or path at define time
BLDINDEX - Build an alternate index
DEFINE ALTERNATE INDEX- Define an alternate Index
DEFINE PATH - Define path that relates an alternate index to the base
cluster
DELETE - Remove a catalog entry for a cluster, alternate index or path
LISTCAT - List information about datasets
PRINT - Print the contents of a VSAM or non-VSAM file
REPRO - Copy records from one file to another
VSAM - KSDS
Define a KSDS using IDCAMS utility -
//STEP010 EXEC PGM=IDCAMS
//SYSPRINT DD SYSOUT=*
//SYSIN DD *
DEFINE CLUSTER (NAME (CUSTOMER.MASTER.FILE) -
OWNER (SXP729) -
INDEXED -
RECORDSIZE(200 200) -
NONSPANNED -
KEYS(9 12) -
VOL(DISK) -
FREESPACE(20 10) -
SHAREOPTIONS(3) -
IMBED) -
DATA (NAME(CUSTOMER.MASTER.FILE.DATA) -
CYL(50 5) -
CISZ(4096)) -
INDEX (NAME(CUSTOMER.MASTER.FILE.INDEX))
/*


VSAM - KSDS
NAME - Required. Specifies the name of the cluster or the component
OWNER - Optional. Specifies the owner-id of the file
INDEXED - Required. Specifies that youre defining a KSDS file. For ESDS mention
NONINDEXED. For RRDS, specify NUMBERED
RECORDSIZE (Avg, Max) - Optional. Specifies the average and maximum length of the
files record. VSAM assumed (4089 4089) for nonspanned
and (4086 32600) for spanned records
SPANNED / NONSPANNED - Optional. Specifies whether records can span over control
intervals. Nonspanned is default.
KEYS (length, Offset) - Required. Specifies the length and offset of primary key in
KSDS
VOL - Specifies one or more volumes that will contain the cluster
FREESPACE(ci, ca) Optional. Specifies the percentage of space in each control interval
and control area
IMBED - Optional. Moves sequence set records from index storage to the first
track of data storage
SHAREOPTIONS(n) - n=1 Specifies multiple jobs can access the file in Input mode
n=2 Specifies one job can use in output mode and multiple jobs in input mode
n=3 Specifies multiple jobs can be use in input or output mode

VSAM - KSDS
Loading a KSDS - Loading of a KSDS file is achieved by using the IDCAMS with
REPRO command. The REPRO command copies one file to another.
//S60001U EXEC PGM=IDCAMS
//SYSPRINT DD SYSOUT=*
//SYSUDUMP DD SYSOUT=*
//INDD1 DD DSN=TEST.CUST.SEQ.FILE,DISP=SHR
//OUTDD1 DD DSN=CUSTOMER.MASTER.FILE,DISP=SHR
//SYSIN DD *
REPRO INFILE(INDD1) OUTFILE(OUTDD1)
/*
The above JCL step loads the KSDS VSAM file in DD statement OUTDD1 from the sequential
file given in INDD1
The input file should be in ascending collating sequence on the primary key. If this werent
so, REPRO would give an error message whenever it found out of sequence record in the
input dataset. After four such operations the load is aborted by IDCAMS
Since sequential files are not always sorted in the order of primary key an external SORT
must be performed before loading the data into KSDS
VSAM - KSDS
COBOL for KSDS datasets - Cobol processes VSAM KSDS in three ways SEQUENTIAL,
RANDOM & DYNAMICALLY
Sequential Processing - When a KSDS is processed sequentially, its records are
accessed from the first in ascending order of the files KEY field. Processing usually begins
from the first record and will continue until the last
SELECT Statement for sequential access
SELECT FILE-NAME ASSIGN TO DDNAME
ORGANIZATION IS INDEXED
ACCESS MODE IS SEQUENTIAL
RECORD KEY IS DATA-NAME-1
FILE STATUS IS DATA-NAME-2
FD statement for sequential access
FD FILE-NAME
LABEL RECORDS ARE STANDARD
PROCEDURE DIVISION Statements
OPEN {INPUT, OUTPUT, I-O} FILE-NAME
VSAM - KSDS
START FILE-NAME [ KEY IS { =, >, <, NOT=, NOT>, NOT< } DATA-NAME ]
[ INVALID KEY Imperative Statement]
Start statement is used if the sequential processing is to be started with a record other
than the first record in the file
START statement can be used when the file is opened in Input or I-O modes
KEY clause is optional if omitted, reads the record with key present in the RECORD KEY
field
READ FILE-NAME [NEXT] RECORD [INTO IDENTIFIER]
[AT END Imperative Statement]
WRITE RECORD-NAME [FROM IDENTIFIER] [INVALID KEY Imperative
Statement]
REWRITE RECORD-NAME [FROM IDENTIFIER]
[INVALID KEY Imperative Statement]
To execute a REWRITE command for A KSDS, the file must be opened in I-O & the
record to be re-written must be read before being re-written
DELETE FILE-NAME RECORD [INVALID KEY Imperative Statement]
Deletes the record read by last READ statement
CLOSE FILE-NAME
VSAM - KSDS
Random Processing - When an indexed file is processed randomly, records can be
accessed in any order.
To access a record the key value of the record must be placed in the RECORD
KEY field and issue an I/O statement
During Random processing, READ, WRITE, REWRITE & DELETE statements all
depend on the RECORD KEY field
SELECT Statement for Random access
SELECT FILE-NAME ASSIGN TO DDNAME
ORGANIZATION IS INDEXED
ACCESS MODE IS RANDOM
RECORD KEY IS DATA-NAME-1
FILE STATUS IS DATA-NAME-2
FD statement for sequential access
FD FILE-NAME
LABEL RECORDS ARE STANDARD
PROCEDURE DIVISION Statements
OPEN {INPUT, OUTPUT, I-O} FILE-NAME

VSAM - KSDS

READ FILE-NAME RECORD [INTO IDENTIFIER] [INVALID KEY Imperative
Statement]
WRITE RECORD-NAME [FROM IDENTIFIER] [INVALID KEY Imperative
Statement]
REWRITE RECORD-NAME [FROM IDENTIFIER] [INVALID KEY Imperative
Statement]
To execute a REWRITE command for A KSDS, the file must be opened in I-O & the
record to be re-written must be read before being re-written
DELETE FILE-NAME RECORD [INVALID KEY Imperative Statement]
Deletes the record read by last READ statement
CLOSE FILE-NAME
All the above statements need the RECORD KEY field to be populated before issuing any of
the I/O statements

VSAM - KSDS
Random Processing - Some program require to use a mixture of sequential and
random access. This can be referred as Dynamic Processing.
SELECT Statement for Random access
SELECT FILE-NAME ASSIGN TO DDNAME
ORGANIZATION IS INDEXED
ACCESS MODE IS DYNAMIC
RECORD KEY IS DATA-NAME-1
FILE STATUS IS DATA-NAME-2
FD statement for sequential access
FD FILE-NAME
LABEL RECORDS ARE STANDARD
PROCEDURE DIVISION Statements
OPEN {INPUT, OUTPUT, I-O} FILE-NAME
When the access mode is mentioned as DYNAMIC, the I/O statements for both sequential
processing and random processing can be used

VSAM - KSDS
VSAM Error processing : VSAM error processing is handled by checking the file status
codes as follows
FILE STATUS ERROR DESCRIPTION
00 Successful Processing
02 Valid Alternate Key
10 End of File reached
21 Record out of sequence
22 Duplicate Key
23 Record Not found
24 Space problem for Write
30 Un Correctable I/O error
90 VSAM Logic Error
91 Password failure
92 File Already Opened
93 Not Enough virtual Storage
95 Conflicting file attributes
97 File not closed by previous job
ALTERNATE INDEX
DEFINE AIX (NAME (EMPMAST.FILE.SSN.AIX) -
RELATE(EMPLOYEE.MASTER.FILE) -
OWNER(SXP729) -
KEYS (9 12) -
UNIQUEKEY -
NOUPGRADE -
FREESPACE (20 10) -
SHAREOPTIONS(3) -
IMBED ) -
DATA (NAME(EMPMAST.FILE.SSN.AIX.DATA) -
CYL(1 1) -
CISZ(4096) -
INDEX (NAME(EMPMAST.FILE.SSN.AIX.INDEX)
DEFINE PATH (NAME(EMPMAST.FILE.SSN.PATH) -
PATHENTRY(EMPMAST.FILE.SSN.PATH.AIX) -
UPDATE )
BLDINDEX INDATASET(EMPMAST.FILE.SSN) -
OUTDATASET(EMPMAST.FILE.SSN.PATH)

The RELATE parameter specifies the name of the base cluster to which this alternate index
is related
When records of a base cluster are processed thru alternate index, the base cluster or the
alternate index is not not accessed directly. Instead, another catalogue entry called PATH is
used

ALTERNATE INDEX
PATHENTRY parameter relates the alternate index to which the path is related
Once Alternate Index and paths for a file have been defines, a BLDINDEX command needs
to be issued to create alternate key entries to access the base cluster
The BLDINDEX command reads all the records in the base cluster and from those records it
extracts the data it needs to build the alternate index
The reads alternate key and corresponding primary key for each record in the base cluster.
Sorts the record pairs into ascending alternate key sequence and writes the pairs into
alternate index
INDATASET parameter identifies the name of the base cluster
OUTDATASET identifies the name of the alternate index or path directly.

ALTERNATE INDEX IN COBOL
SELECT Statement for KSDS with alternate index access
SELECT FILE-NAME ASSIGN TO DDNAME
ORGANIZATION IS INDEXED
ACCESS MODE IS SEQUENTIAL/RANDOM/DYNAMIC
RECORD KEY IS DATA-NAME-1
FILE STATUS IS DATA-NAME-2
ALTERNATE RECORD KEY IS DATA-NAME-3

Before issuing any of the I/O statements the alternate key must be moved to DATA-NAME-3.
After moving the Alternate Key value, any I-O statement can be issued and the KSDS will
read the data by accessing the alternate Index
When an alternate key is being used in the program, the PATH file should be coded as one
of the DD names for the step in the JCL that executes the program

VSAM - ESDS
Define a ESDS using IDCAMS utility -
//STEP010 EXEC PGM=IDCAMS
//SYSPRINT DD SYSOUT=*
//SYSIN DD *
DEFINE CLUSTER (NAME (CUSTOMER.MASTER.FILE) -
OWNER (SXP729) -
NONINDEXED -
RECORDSIZE(200 200) -
VOL(DISK) -
DATA (NAME(CUSTOMER.MASTER.FILE.DATA) -
CYL(50 5) -
CISZ(4096))
/*
VSAM - ESDS
ESDS Processing - VSAM entry sequenced datasets have similar processing to
that Non-VSAM sequential files. The code to ESDS is similar to that of a Non-VSAM
sequential files
SELECT Statement for Random access
SELECT FILE-NAME ASSIGN TO DDNAME
ORGANIZATION IS SEQUENTIAL
ACCESS MODE IS SEQUENTIAL
FILE STATUS IS DATA-NAME-2
FD statement for sequential access
FD FILE-NAME
LABEL RECORDS ARE STANDARD
PROCEDURE DIVISION Statements
OPEN {INPUT, OUTPUT, I-O,EXTEND} FILE-NAME
READ FILE-NAME [NEXT] RECORD [INTO IDENTIFIER]
[AT END Imperative Statement] [NOT AT END Imperative Statement]
WRITE RECORD-NAME [FROM DATA-NAME]
REWRITE RECORD-NAME [FROM DATA-NAME]
CLOSE FILE-NAME