TSO-ISPF JCL COBOL VSAM DB2 CICS IMS-DB Tools Articles Forum Quiz Interview Q&A

VSAM - Key Sequenced Data Set


KSDS is abbreviated as Key Sequenced Data Set. A key-sequenced data set (KSDS) is more complex than ESDS and RRDS but is more useful and versatile.

When you define KSDS cluster, We must code INDEXED inside the DEFINE CLUSTER command. KSDS cluster consists of following two components.

  1. Index

    The index component of the KSDS cluster contains the list of key values for the records in the cluster with pointers to the corresponding records in the data component. Index component refers the physical address of a KSDS record. This relates the key of each record to the record's relative location in the data set. When a record is added or deleted, this index is updated accordingly.

  2. Data

    The data component of the KSDS cluster contains the actual data. Each record in the data component of a KSDS cluster contains a key field with same number of characters and occur in the same relative position in each record.


Benifits of KSDS:

  • Records within KSDS data set are always kept sorted by key-field. Records are stored in ascending, collating sequence by key.

  • Records can be accessed sequentially and direct access is also possible.

  • Records are identified using a key. The key of each record is a field in a predefined position within the record. Each key must be unique in KSDS dataset. So duplication of records is not possible.

  • When new records are inserted, the logical order of the records depends on the collating sequence of the key field.

  • Records in KSDS dataset can be of fixed length or variable length.

  • KSDS can be used in COBOL programs like any other file. We will specify the file name in JCL and we can use the KSDS file for processing inside program. In COBOL program specify file organization as Indexed and you can use any access mode (Sequential, Random or Dynamic) with KSDS dataset.


KSDS File Structure:

In order to search for a particular record, we give a unique key value. Key value is searched in the index component. Once key is found the corresponding memory address which refers to the data component is retrieved. From the memory address we can fetch the actual data which is stored in the data component. Following example shows the basic structure of index and data file.

Index Component:
KeyMemory Address
Key 11000
Key 25000
Key 33000
Key 42000

Data Component:
Memory AddressKeyData Field
3000Key 3Record 3
5000Key 2Record 2
2000Key 4Record 4
1000Key 1Record 1

Syntax: Define KSDS Cluster

DEFINE CLUSTER                             -
   (NAME(ksds-file-name)                    -
   BLOCKS(number)                           -
   VOLUMES(volume-serial)                   -
   INDEXED                                  -
   KEYS(length offset)                      -
   RECSZ(average maximum)                   -
   [FREESPACE(CI-Percentage,CA-Percentage)] -
   CISZ(number)                             -
   [READPW(password)]                       -
   [FOR(days)|TO(date)]                     -
   [UPDATEPW(password)]                     -
   [REUSE / NOREUSE])                       -
DATA                                       -
   NAME(ksds-file-name.data))               -
INDEX                                      -
   (NAME(ksds-file-name.index))

The syntax parameter description remains the same as we explained in VSAM - Define Cluster Syntax chapter.


Example:

Following example shows how to create an KSDS cluster in JCL using IDCAMS utility.

//JOBNAME1 JOB CLASS=C,PRTY=10,MSGCLASS=C,MSGLEVEL=(1,1),NOTIFY=&SYSUID
//*
/STEP001   EXEC PGM=IDCAMS
//SYSPRINT DD  SYSOUT=*
//SYSIN    DD  *
  DEFINE CLUSTER                              -
      (NAME(racfid.VSAM.KSDSFILE.TEST)        -
      INDEXED                                 -
      KEYS(6 1)	                              -
      RECSZ(80 80)                            -
      TRACKS(1,1)                             -
      CISZ(4096)                              -
      FREESPACE(3 3) )                        -
  DATA                                        -
      (NAME(racfid.VSAM.KSDSFILE.DATA.TEST))  -
  INDEX                                       -
      (NAME(racfid.VSAM.KSDSFILE.INDEX.TEST))
/* 

If you execute the above JCL. Job should complete successfully with MAXCC = 0 and it will create racfid.VSAM.KSDSFILE.TEST VSAM file.


Syntax: Delete KSDS Cluster

KSDS cluster is deleted using IDCAMS utility. DELETE command removes the entry of the VSAM cluster from the catalog and optionally removes the file, thereby freeing up the space occupied by the object.

DELETE data-set-name CLUSTER
    [ERASE / NOERASE]
    [FORCE / NOFORCE]
    [PURGE / NOPURGE]
    [SCRATCH / NOSCRATCH]

The syntax parameter description remains the same as we explained in VSAM - Delete Cluster Syntax chapter.

Example:

Following example shows how to delete an KSDS cluster in JCL using IDCAMS utility.

//JOBNAME1 JOB CLASS=C,PRTY=10,MSGCLASS=C,MSGLEVEL=(1,1),NOTIFY=&SYSUID
//*
//STEP001  EXEC PGM=IDCAMS
//SYSPRINT DD  SYSOUT=*
//SYSIN    DD  *
   DELETE racfid.VSAM.KSDSFILE.TEST CLUSTER
/*

If you execute the above JCL. Job should complete successfully with MAXCC = 0 and it will delete racfid.VSAM.KSDSFILE.TEST VSAM Cluster.


If you have any doubts or queries related to this chapter, get them clarified from our Mainframe experts on ibmmainframer Community!

Are you looking for Job Change? Job Portal