KSDS


  • KSDS is a short form of Key Sequential Data Set.
  • KSDS is very commonly used dataset among all types of VSAM datasets.
  • KSDS datasets have the most similar features as ISAM and contain DATA and INDEX components. The DATA component has all the data records in a logical ascending order based on the key. The INDEX component has pointers to all data records (every data record has a corresponding pointer) to access data.
  • KSDS allows fixed-length or variable-length records.
  • Records are initially loaded in ascending order by key-value and can be accessed (retrieved or deleted or inserted) by using a field, a key, or an RBA.
  • A key identifies each record uniquely, and a key is a field in a predefined position within the logical record.
  • The records are always inserted in sorted order based on the key value. For example, let us assume we are trying to insert a record with a 100 key value; it inserts after the record with the highest value of less than 100.
  • The records are reorganized as specified below for each operation -
    • If a record is newly inserted, then the records after the newly inserted record will be reorganized to maintain the logical order for easy retrieval.
    • If any record gets deleted, the records after the deleted one will be reorganized to maintain the logical order for easy retrieval.
    • If any record is updated, there will be no change in positions.

Key Restrictions -


  • Key length should be the same in all logical records and it can be from 1 to 255 bytes.
  • The key value should be unique and no duplicates allowed in all logical records.
  • Key cannot get altered once it is defined while creating the file.

Access Types -


VSAM KSDS always uses the INDEX component to access records from the DATA component in ascending or descending order by key. KSDS supports three types of accesses, and those are -

Access TypeDescription
Sequential Access
  • Sequential access used to retrieve (reading) or inserting (writing) the records sequentially.
  • When retrieving (reading) or inserting (writing) records, we need to provide the key value as an input.
  • Sequential access is faster when accessing records in sequential order.
Random(Direct) Access
  • Direct access uses to retrieve the records directly by using key values. The application program should provide a key value for each record to be processed (read or written).
  • The key value can be full or generic (partial).
  • The generic (partial) key is the high-order portion of a full key. For example, We might plan to retrieve all records whose keys begin with MTH (where MTH is the generic key).
Dynamic Access
or
Skip Sequential
  • Skip-sequential access is a combination of sequential and random access.
  • The application provides a key value, and all records are processed sequentially from the located record.

All the above access types supports loading, retrieving, updating, inserting, and deleting records in an existing KSDS dataset.

IDCAMS DEFINE CLUSTER command with INDEXED parameters used to create KSDS dataset.

Syntax - JCL for creating KSDS dataset with minimum parameters

//job-card
//*
//STEP01  EXEC PGM=IDCAMS
  DEFINE CLUSTER(NAME(userid.CLUSTER.NAME)	- 
    RECORDSIZE(avg max)    			-                     
    CYLINDERS(primary secondary)    -                     
    FREESPACE(primary secondary)    -                     
    KEYS(length,offset)            	-                     
    CISZ(ci-size)           		-                     
    VOLUMES(volume-name)      		-                     
    INDEXED							-                     
    REUSE )             			-                     
  INDEX(NAME(userid.CLUSTER.NAME.INDEX))	-        
  DATA(NAME(userid.CLUSTER.NAME.DATA))             	 
/*

Refer IDCAMS DEFINE command for full set of parameters.

Fixed-length RRDS -


Create Fixed-length KSDS Example -


Scenario - Creating KSDS with fixed-length records of size 47 bytes.

JCL -

----+----1----+----2----+----3----+----4----+----5----+
...
//STEP01   EXEC PGM=IDCAMS
//SYSIN    DD  *
  DEFINE CLUSTER(NAME(MATEGJ.TEST.KSDS)  -
    RECORDSIZE(47,47)    -
    CYLINDERS(2,1)       -
    FREESPACE(10,20)     -
    KEYS(3,0)            -
    CISZ(4096)           -
    VOLUMES(DEVHD4)      -
    INDEXED              -
    REUSE  )             -
  INDEX(NAME(MATEGJ.TEST.KSDS.INDEX)) -
  DATA(NAME(MATEGJ.TEST.KSDS.DATA))
/*
...

How to use Fixed-length KSDS in the COBOL program?


Scenario - Define the fixed-length KSDS file for RANDOM access in the COBOL program.

Let us assume MATEGJ.TEST.KSDS is the KSDS file with the record length 47, and it is defined like below in the COBOL program.

In Environment Division -

ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
	SELECT KSDS-1 ASSIGN to INPUT.
	ORGANIZATION IS INDEXED.
	ACCESS MODE IS RANDOM.
	RECORD KEY IS KSDS-KEY-1.
	FILE STATUS IS file-status1.

In Data Division -

DATA DIVISION.
FILE SECTION.
FD KSDS-1.
	RECORD CONTAINS 47 CHARACTERS.
	BLOCK CONTAINS 470 CHARACTERS.
	DATA RECORD is KSDS-RECORD-1.
	RECORDING MODE IS F.

01 KSDS-RECORD-1.
	05 KSDS-KEY-1    PIC X(03).
	05 KSDS-REC-PART PIC X(44).

In the above example, KSDS-1 is the file-name used to represent the KSDS file in the COBOL program. INPUT is the DDNAME used to map with the JCL DDNAME specified corresponding to the KSDS file. For example -

//INPUT DD DSN=MATEGJ.TEST.KSDS,DISP=SHR

KSDS-RECORD-1 represents the logical record used to insert the data to a file or read the data from a file.

Variable-length RRDS -


Create Variable-length KSDS Example -


Scenario - Creating KSDS with variable-length records of average size 47 and maximum size 67 bytes.

JCL -

----+----1----+----2----+----3----+----4----+----5----+
...
//STEP01   EXEC PGM=IDCAMS
//SYSIN    DD  *
  DEFINE CLUSTER(NAME(MATEGJ.TEST.VKSDS)  -
    RECORDSIZE(47,67)    -
    CYLINDERS(2,1)       -
    FREESPACE(10,20)     -
    KEYS(3,0)            -
    CISZ(4096)           -
    VOLUMES(DEVHD4)      -
    INDEXED              -
    REUSE  )             -
  INDEX(NAME(MATEGJ.TEST.VKSDS.INDEX)) 	-
  DATA(NAME(MATEGJ.TEST.VKSDS.DATA))
/*

How to use Variable-length KSDS in the COBOL program?


Scenario - Define the variable-length KSDS file for DYNAMIC access in the COBOL program.

Let us assume MATEGJ.TEST.VKSDS is the variable-length KSDS file with record length 47 to 67, and it is defined like below in the COBOL program.

In Environment Division -

ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
	SELECT KSDS-2 ASSIGN to INPUTV.
	ORGANIZATION IS INDEXED.
	ACCESS MODE IS DYNAMIC.
	RECORD KEY IS KSDS-KEY-2.
	FILE STATUS IS file-status2.

In Data Division -

DATA DIVISION.
FILE SECTION.
FD KSDS-2.
	RECORD CONTAINS 47 TO 67 CHARACTERS.
	BLOCK CONTAINS 470 TO 670 CHARACTERS.
	DATA RECORD is KSDS-RECORD-2.
	RECORDING MODE IS V.

01 KSDS-RECORD-2.
	05 KSDS-KEY-2    PIC X(03).
	05 KSDS-REC-PART PIC X(64).

In the above example, KSDS-2 is the file-name used to represent the KSDS file in the COBOL program. INPUT is the DDNAME used to map with the JCL DDNAME specified corresponding to the KSDS file. For example -

//INPUTV DD DSN=MATEGJ.TEST.VKSDS,DISP=SHR

KSDS-RECORD-2 represents the logical record used to insert the data to a file or read the data from a file.

Loading Data to KSDS -


Loading data to KSDS can be done in three ways -

  • Using utilities.
  • Using File Management Tools[Covers in tools section in near future].
  • Through programmatically[Covered as part of COBOL tutorial ].

Using utilities -


IDCAMS utility is used to load the data to the KSDS file from another file or instream data.

Scenario - Copy data from PS file to KSDS file.

Input PS File - MATEGJ.TEST.DATA

JCL -

----+----1----+----2----+----3----+----4----+----5----+
...
//STEP01   EXEC PGM=IDCAMS
//SYSPRINT DD   SYSOUT=*
//INPUT    DD   DSN=MATEGJ.TEST.DATA,DISP=SHR
//OUTPUT   DD   DSN=MATEGJ.TEST.KSDS,DISP=SHR
//SYSIN    DD   *
      REPRO    -
           INFILE(INPUT)   -
           OUTFILE(OUTPUT)
/*
...
Note! The flat file(PS) and KSDS file should have the same record length (47 in the above example) to load the data successfully.

Delete KSDS Example -


Scenario - Delete KSDS using the IDCAMS utility.

Code -

----+----1----+----2----+----3----+----4----+----5----+
...
//STEP01   EXEC PGM=IDCAMS
//SYSPRINT DD   SYSOUT=*
//SYSIN    DD   *
      DELETE 'MATEGJ.TEST.KSDS'
/*
...
Note! If the file is already deleted and trying to delete the file again, we may receive MAXCC as 08. So, verify the file once before proceeding for delete.

Advantages -


  • Easy and fast creation.
  • Complete access to the dataset.
  • Can have an alternate index.
  • Can access the records sequentially, randomly, and dynamically (Skip-sequential).

Disadvantages -


  • Records should be in sorted order based on the key before loading.

Usage -


KSDS used when -

  • Applications require to access the records in different access modes using a key field.
  • Applications require both direct and sequential access.
  • Applications use high-level languages that do not support RBA.
  • CICS appication preferred dataset.