VSAM, A BRIEF INTRODUCTION ....................................................................... 1 Types of VSAM datasets ............................................................ 1 Internal Organization ............................................................... 2 Alternate Index .................................................................... 3 VSAM FILE IN CICS ................................................................ 3 USING VSAM FILES IN COBOL ...................................................... 6 Introduction Introduction VSAM, fully known as Virtual Storage Access Method is a data management system introduced by IBM, as part of modern IBM operating systems like MVS in 1970s.VSAM software resides in virtual storage along with the application program that needs it services for the manipulation of data on the direct storage access device (DASD) Types of VSAM datasets VSAM provides three types of dataset organization: Key Sequenced Data Set (KSDS), where records are identified and sequenced on a key field. Key field is a part of data record which uniquely identifies that record from all other records in the dataset. KSDS datasets are similar to Indexed Sequential Access Method (ISAM) datasets, with many of the same characteristics, but also having distinct advantages over ISAM. A KSDS consists of two physical components on DASD. Data Component Index Component Data component contains the records that hold the user data including the Key field. The records in Data component are stored in ascending sequence of the key field. Index component contains the key field and the pointer to the user data to which the key field belongs. These components are jointly called base cluster. The following figure shows the layout of index component and the Data Component. (Index Component) Key Pointer field (Data Component) Key Field Data Records KSDS allows both random and sequential, retrieval and insertion of records in the dataset. Insertion is achieved by providing free space between the records at the time of loading. Excess of random insertion/deletion of records may result in records becoming physically out of sequence but records remain in logical sequence when they are accessed through the index. Physical sequencing of records help in efficient sequential retrieval of records and to maintain that KSDS dataset needs occasional reorganization of the file. Entry Sequenced Data Set (ESDS), as the name depicts , the records are sequenced in the order in which they are loaded in the dataset and identified by specifying its physical location - the byte address of the first data byte of each record in relationship to the beginning of the dataset. A new insertion in the ESDS is always appended at the end of the dataset but allows both random and sequential retrieval of the records. Few Tips while using ESDS: Even if ESDS allows variable length record, while updation the record length cannot be changed. Records in ESDS cannot be physically deleted. ESDS are usually used by online programs as audit-trail or data entry file, which can later be used batch programs to update the master files. An ESDS only has the data component. ESDS datasets are similar to Basic Sequential Access Method (BSAM) or Queued Sequential Access Method (QSAM) datasets. Relative Record Data Set (RRDS), where each record is identified for access by specifying its record number - the sequence number relative to the first record in the dataset. RRDS datasets are similar to Basic Direct Access Method (BDAM) datasets. Internal Organization Control Intervals In VSAM, the smallest unit of data transferred between I/O buffers and dataset is defined as a control interval. A control interval is basically a physical entity on disk and contains records, control interval description field (CIDF 4 Bytes), Record description field (RDF 3 bytes) and (in the case of KSDS clusters) possibly free space which may later be used to contain inserted records. With ESDS clusters, CI does not contain any free space as no insertion is made between records. CI is completely filled before record is written into the next control interval in sequence. With RRDS clusters, control intervals are filled with fixed-length slots, each containing either an active record or a dummy record. Slots containing dummy records are available for use when new records are added to the dataset. Control Areas A control area consists of two or more control intervals. In case of KSDS clusters some CI are left free for insertion purpose. For ESDS and RRDS clusters, control areas are filled with control intervals that contain records. Spanned Records: When the length of record is more then the length of CI Size minus 7 then the records are contained in more then one CI such a record is called Spanned Record. Free space left by spanned record is left unused. Alternate Index: An AIX is a file which allows access to a VSAM dataset by a key other than the primary key. In case of KSDS, AIX contains primary key field of the base cluster and alternate key field of the base cluster. When the search on the alternate key is made then, AIX file is accessed. The record with the same value of alternate index as given by user is found in AIX file. This in turn gives the primary key corresponding to the record. After this base cluster is searched randomly for the primary key and all the values corresponding to primary key are retrieved. Search on alternate keys are slower than the search on the primary key. Even if there is no concept of primary key in ESDS it supports Alternate Key. In this case Alternate cluster contains the Key field and RBA of the record. Alternate index may be on unique or non-unique field. VSAM FILE IN CICS All Online Programs on Mainframe run in CICS. So, usage of VSAM files in CICS is an important concept. First of all there must be a FCT entry of the file that is required to be used in CICS program. Once there is a FCT entry, then a program can access the file using logical name. There is always an interaction between logical and physical name of the file in JCL. Different systems use different commands to show JCL. In VSE, typing LBL f3 gives the JCL indicating the physical and logical name of the file. Once file has FCT entry, following functions can be used on the file: BROWSING FUNCTIONS STARTBR READNEXT READPREV RESETBR ENDBR STARTBR As the name indicates this means start browsing the file. In fact, this locates the pointer on a particular record on the file from which reading of records has to begin SYNTAX EXEC CICS STARTBR DATASET ( dataset-name) RIDFLD ( key-field) [ GTEQ, EQUAL] END-EXEC. Dataset-name: This is the Logical name of the file. Key-Field : This is the record field, which is to be located on the file. GTEQ : If specified, key is located on any record with key equal to or greater than the key-field specified. Default is GTEQ if nothing is specified. READNEXT This function reads the record already located by STARTBR option. Once located, records can be read in sequence. SYNTAX EXEC CICS READNEXT DATASET (dataset-name) INTO (Data-name1) END-EXEC. Data-name1: This indicates the field, which will contain the record that has been read. READPREV This function is used to browse a file in backward direction. This also follows STARTBR function. SYNTAX EXEC CICS READPREV DATASET (dataset-name) INTO (Data-name1) END-EXEC. Data-name1: This indicates the field, which will contain the record that has been read. RESETBR This function is used to reset the browser once browsing is in progress. SYNTAX EXEC CICS RESETBR DATASET (dataset-name) RIDFLD (Data-name1) END-EXEC. ENDBR This option is used to end the browsing. SYNTAX EXEC CICS ENDBR DATASET (dataset-name) END-EXEC. OTHER FUNCTIONS Other functions involved for datasets in CICS are READ DATASET WRITE DATASET REWRITE DATASET DELETE DATASET READ DATASET: This statement is used to read a dataset in random mode. SYNTAX EXEC CICS READ DATASET ( cics-file-name) INTO ( cobol-data-record) RIDFLD (key-field) [GTEQ, EQUAL] END-EXEC WRITE DATASET This involves writing the record on to the file. SYNTAX EXEC CICS WRITE DATASET (cics-file-name) FROM (cobol-data-record) RIDFLD (key-field) END-EXEC REWRITE DATASET This means replacing the contents of the record which has a key matching with the key passed. SYNTAX EXEC CICS REWRITE DATASET (cics-file-name) FROM (cobol-data-record) END-EXEC DELETE DATASET This is used to delete record from the file which has same key as the key passed. SYNTAX EXEC CICS DELETE DATASET (cics-file-name) RIDFLD (key-field) END-EXEC Using VSAM Files In Cobol We will consider SELECT statement for file. This declaration is made in the FileControl Section of the COBOL program. SYNTAX SELECT File-Name ASSIGN TO Assignment-name ORGANIZATION IS INDEXED ACCESS MODE IS ACCESS-MODE1 RECORD KEY IS DATA-NAME1 FILE STATUS IS DATA-NAME2 File-Name This is the logical name that will be referred in COBOL program. From now we assume file name for our documentation as EMPLOYEE Assignment-Name This is same as the DD name of the dataset in the JCL. Organization is Indexed indicates that this dataset is a KSDS. Access-mode This specifies the manner in which the file will be accesses. It can be accessed either in Sequential mode or Random mode or Dynamic Mode. RECORD KEY This is actually the primary key field of the KSDS. This is coded in the FD clause. We will use EMP-REC as Record key for documentation. File Status This is equivalent to the return code after any access. Example: If you open a dataset and get return code not equal to zeroes, this means that there has been some error in opening of the dataset. After making Declaration in the FILE-CONTROL Section, we will consider the declaration in the FILE SECTION of DATA DIVISION. SYNTAX FD EMPLOYEE. RECORDING MODE IS F LABEL RECORDS ARE OMITTED RECORD CONTAINS 80 CHARACTERS. 01 EMP-REC PIC X (80). It is mandatory to mention LABEL RECORDS in the FD Section. Processing Of File In Procedure Division We can open file in either input or output or I-O or Extend mode. We will discuss each of the modes: - OUTPUT Mode This allows simply writing on a file. IF a file is used in this mode, then all the existing records in the file will be deleted. This means that Output mode places the pointer on the first position on the file. It does not append records on the file. I-O Mode This allows the user to add, delete and rewrite the records in the dataset. SYNTAX EMPLOYEE INPUT. INPUT Mode This will allow only Read or Browse function on the dataset. EXTEND Mode This allows the user to append the records in the dataset. Reading From VSAM Files In Sequential Mode SYNTAX READ EMPLOYEE [ INTO DATA-NAME1] [AT END imperative statement] Both INTO clause and AT END clause are optional. Imperative Statement indicates the statements to be executed in case the end of file is reached. Reading From VSAM Files In Random Or Dynamic Mode SYNTAX READ EMPLOYEE [ INTO DATA-NAME1] [ INVALID KEY imperative statement ] Imperative statement will be executed if the key is not found. Important concept in reading files is that before any kind of read statement you must move the record to be read in the RECORD KEY area in the FD statement. In our example value must be move in EMP-REC before executing the read statement. Writing VSAM Files In Sequential Mode In case of writing records in VSAM file accessed in sequential mode, it is necessary that the record, which is to be written has primary key greater than the record with the highest primary key. SYNTAX WRITE EMPLOYEE [FROM DATA-NAME1] [INVALID KEY imperative statement] INVALID KEY may be the case in which either the record does not possess the highest key (STATUS CODE 21) or the record already exists in the VSAM file. Writing In VSAM Files In Random Or Dynamic Mode In case of writing records in VSAM file accessed in dynamic or random mode, it is not necessary that the record which is to be written has primary key greater than the record with the highest primary key. SYNTAX WRITE EMPLOYEE [FROM DATA-NAME1] [INVALID KEY imperative statement] INVALID KEY means record already exists in the VSAM file. REWRITING IN VSAM FILES This enables the user to change the existing record. Normal procedure is: First read the record, modify the contents and then write it back using REWRITE statement. The only restriction is that Primary key cannot be modified. If Access Mode is Sequential then it is necessary to read the record, in other case it is not necessary to read the record. SYNTAX REWRITE EMPLOYEE [FROM DATA-NAME1] [INVALID KEY imperative statement] Deleting From VSAM Files Records can be physically deleted from VSAM files. This gets the space reclaimed and provides the space for other records. SYNTAX DELETE EMPLOYEE [INVALID KEY imperative statement] BROWSING VSAM FILES Sometimes, it is necessary to browse VSAM files after a particular value i.e. not from beginning to end but from some position till end. This is possible using combination of START and READ NEXT statement. START This actually positions the location from which reading is to be done. SYNTAX START EMPLOYEE [KEY IS (= OR > OR <) DATA-NAME] [INVALID KEY is imperative statement] READ NEXT This starts reading the record in sequential manner including record on which positioning is done using START statement. SYNTAX READ EMPLOYEE NEXT [INTO DATA NAME] [AT END imperative statement] the