560 likes | 667 Views
VSAM KSDS and COBOL. Department of Computer Science Northern Illinois University August 2005. Some of the illustrations are from VSAM: Access Method Services and Programming Techniques by James Martin. Processing a KSDS. KSDS can be processed 3 ways Sequentially
E N D
VSAM KSDS and COBOL Department of Computer Science Northern Illinois University August 2005 Some of the illustrations are from VSAM: Access Method Services and Programming Techniques by James Martin
Processing a KSDS • KSDS can be processed 3 ways • Sequentially • Records accessed in key sequence • Randomly • Records accessed via a specified key value • Dynamically • Records accessed both sequentially and randomly
Sequentially Processing a KSDS • Access records in ascending order on the file’s key field • Usually begin with the first record and process to the end
File-Control for Sequential KSDS File-Control. 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.
File-Control for Sequential KSDS • File-Control. SELECT file-name where file-name is the name used for the file throughout the program
File-Control for Sequential KSDS • File-Control. ASSIGN to ddname where ddname is the ddname in the JCL used to refer to the KSDS file
File-Control for Sequential KSDS • File-Control. ORGANIZATION is INDEXED where INDEXED specifies that this file is a KSDS
File-Control for Sequential KSDS • File-Control. ACCESS MODE is SEQUENTIAL where SEQUENTIAL specifies that the processing of the KSDS will be in order on the key field of the file
File-Control for Sequential KSDS • File-Control. RECORD KEY is data-name-1 where data-name-1 is the file’s key field and the records are going to be accessed ascending on this field data-name-1 must appear in the file’s record description in the FILE SECTION
File-Control for Sequential KSDS • File-Control. FILE STATUS is data-name-2 where data-name-2 is a filed you specify in which VSAM provides information about each I/O operation VSAM places a return code in data-name-2 for the application program to determine the result of the I/O operation
FD Statement in KSDS FD KSDS-file LABEL RECORDS ARE [OMITTED or STANDARD]. Treated as a comment but is required
Procedure Division in KSDS Sequential Processing • OPEN statement • START statement • READ statement • WRITE statement • REWRITE statement • DELETE statement • CLOSE statement
KSDS OPEN Statement • OPEN statement (sequentially) • Must open all files • Loading sequentially • OPEN OUTPUT ksds-file. • Retrieving sequentially • OPEN INPUT ksds-file.
KSDS START Statement • START statement • Used to start sequential processing with a record other than the first record in the file • Use with a file that is opened as input or dynamically (I-O) and processing sequentially • To use, place a value in the record key area
KSDS START Statement • START statement START vsam-file [KEY IS {EQUAL TO = GREATER THAN > NOT LESS THAN NOT < GREATER THAN OR EQUAL TO >= } record-key] [INVALID KEY imperative-1] [NOT INVALID KEY imperative-2] [END-START]
KSDS READ Statement • READ statement (sequential) • To retrieve records in key sequence, open KSDS for either • INPUT or • I-O
KSDS READ Statement • READ statement (sequential) READ file-name RECORD [INTO area] [AT END imperative-1] NOT AT END imperative-2] [END-READ]
KSDS READ Statement • READ statement (sequential) READ file-name [NEXT] Specify NEXT on the READ statement to retrieve records sequentially when file is opened for I-O Omit NEXT when file is opened for I-O and the records will be retrieved randomly based on the value in the RECORD KEY field
KSDS READ Statement • READ statement (sequential) • The key using dynamic access is knowing how to switch from sequential to random access. • The position for sequential retrieval is changed only by a START or a random READ statement.
KSDS READ Statement • READ statement (sequential) • A random READ statement can be used to retrieve a specific record and then can be followed by a sequential READ statement (using the NEXT option)
KSDS WRITE Statement • WRITE statement (sequential) • KSDS must be opened for • OUTPUT for file creation (loading of data) • I-O for file additions • A value must be placed into the primary key field prior to the write
KSDS WRITE Statement • WRITE statement (sequential) WRITE record-name [FROM area] [INVALID-KEY imperative-1] [NOT INVALID-KEY imperative-2] [END-WRITE]
KSDS WRITE Statement • WRITE statement (sequential) • INVALID-KEY imperative-1 is executed when • the value stored in the primary key field prior to issuing the WRITE is equal to that of a record already in the file • an attempt is made to write beyond the boundaries of the file • the primary key is not greater than the primary key of the previous record AND the file is opened for OUTPUT and sequential accessing is specified
KSDS REWRITE Statement • REWRITE statement (sequential) • the KSDS must be opened for I-O • the record to be rewritten needs to be the last record read from the file • must read the record prior to rewriting it
KSDS REWRITE Statement • REWRITE statement (sequential) REWRITE record-name [FROM area] [INVALID KEY imperative-1] [NOT INVALID KEY imperative-2] [END-REWRITE]
KSDS REWRITE Statement • REWRITE statement (sequential) • [INVALID KEY imperative-1] is executed when • the record to be changed does not exist in the file • sequential access is specified and the value placed in the primary key doesn’t equal the primary key fo the previously read record
KSDS DELETE Statement • DELETE statement (sequential) • KSDS opened as I-O • delete the record read in the last READ statement • the space in KSDS is immediately available for reuse
KSDS DELETE Statement • DELETE statement (sequential) DELETE file-record RECORD [INVALID-KEY imperative-1] [NOT INVALID-KEY imperative-2] [END-DELETE]
KSDS DELETE Statement • DELETE statement (sequential) • [INVALID-KEY imperative-1] is executed when • an attempt to delete a record that does not exist is made
KSDS CLOSE Statement • CLOSE statement (sequential) • list the KSDS that is to be closed
KSDS Load Program in COBOL • Go over handout “COBOL JCL & Source Review for Loading KSDS”
KSDS Random Processing • All I/O operations depend upon the record key • Before READ a value must be in the record key • Before WRITE a value must be in the record key
File-Control for Random KSDS File-Control. 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.
File-Control for Random KSDS File-Control. ACCESS MODE is RANDOM The only difference in the SELECT statement for random processing is the word RANDOM access
Procedure Division in KSDS Random Processing • OPEN statement • READ statement • WRITE statement • REWRITE statement • DELETE statement • CLOSE statement
KSDS OPEN Statement • OPEN statement (random) • Must open all files • Retrieving randomly • OPEN INPUT ksds-file. • Writing randomly • OPEN OUTPUT ksds-file. • Reading, rewriting and deleting randomly • OPEN I-O ksds-file.
KSDS READ Statement • READ statement (random) • To retrieve records based upon value in the record key field, open KSDS for either • INPUT or • I-O
KSDS READ Statement • READ statement (random) • To retrieve records based upon value in the record key field, open KSDS for either • INPUT or • I-O
KSDS READ Statement • READ statement (random) READ file-name RECORD [INTO area] [KEY IS data-name-1] [INVALID KEY imperative-1] NOT INVALID KEY imperative-2] [END-READ]
KSDS READ Statement • READ statement (random) [KEY IS data-name-1] The key value of the record that is to be retrieved from the KSDS must be placed in data-name-1 prior to issuing the READ statement.
KSDS READ Statement • READ statement (random) • when invoked comparison is made • between the value of the field specified in the RECORD KEY clause of the SELECT statement (known as “key of reference”) and • the key values of the KSDS records • search is done via the index • appropriate control interval is read into memory and sequentially searched
KSDS WRITE Statement • WRITE statement (random) WRITE record-name [FROM area] [INVALID-KEY imperative-1] [NOT INVALID-KEY imperative-2] [END-WRITE]
KSDS WRITE Statement • WRITE statement (random) • used to add records to a file • KSDS uses the RECORD KEY value to write the record in its proper physical location
KSDS REWRITE statement • REWRITE statement (random) REWRITE record-name [FROM area] [INVALID KEY imperative-1] [NOT INVALID KEY imperative-2] [END-REWRITE]
KSDS REWRITE statement • REWRITE statement (random) • used to update a record in its original physical location • KSDS uses the RECORD KEY value to rewrite the record in its proper physical location
KSDS DELETE Statement • DELETE statement (random) DELETE file-record RECORD [INVALID-KEY imperative-1] [NOT INVALID-KEY imperative-2] [END-DELETE]
KSDS DELETE Statement • DELETE statement (random) • used to delete a record from its physical location • KSDS uses the RECORD KEY value to find the record to delete
KSDS CLOSE Statement • CLOSE statement (random) • list the KSDS that is to be closed
KSDS Dynamic Processing • Processing can be a mix of sequential and random • All the statements remain the same except the SELECT and the sequential READ