Anda di halaman 1dari 55

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

2
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

3
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.

4
File-Control for
Sequential KSDS

• File-Control.
SELECT file-name
where file-name is the name used for the
file throughout the program

5
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

6
File-Control for
Sequential KSDS

• File-Control.
ORGANIZATION is INDEXED
where INDEXED specifies that this file is
a KSDS

7
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

8
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

9
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

10
Common File Status for KSDS

Code Meaning
00 Successful completion
10 End of file reached
21 Sequence error
22 Duplicate key
23 Record not found
24 No more space

11
FD Statement in KSDS

FD KSDS-file
LABEL RECORDS ARE [OMITTED
or STANDARD].

Treated as a comment but is required

12
Procedure Division in
KSDS Sequential Processing

• OPEN statement
• START statement
• READ statement
• WRITE statement
• REWRITE statement
• DELETE statement
• CLOSE statement
13
KSDS OPEN Statement

• OPEN statement (sequentially)


• Must open all files
– Loading sequentially
• OPEN OUTPUT ksds-file.
– Retrieving sequentially
• OPEN INPUT ksds-file.

14
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

15
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]
KEY imperative-2]
[NOT INVALID
[END-START] 16
KSDS READ Statement
• READ statement (sequential)
– To retrieve records in key sequence, open
KSDS for either
• INPUT or
• I-O

17
KSDS READ Statement
• READ statement (sequential)

READ file-name RECORD [INTO area]


[AT END imperative-1]
NOT AT END imperative-2]
[END-READ]

18
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

19
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.

20
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)

21
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

22
KSDS WRITE Statement

• WRITE statement (sequential)

WRITE record-name [FROM area]


[INVALID-KEY imperative-1]
[NOT INVALID-KEY imperative-2]
[END-WRITE]

23
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 24
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

25
KSDS REWRITE Statement

• REWRITE statement (sequential)

REWRITE record-name [FROM area]


[INVALID KEY imperative-1]
[NOT INVALID KEY imperative-2]
[END-REWRITE]

26
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

27
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

28
KSDS DELETE Statement

• DELETE statement (sequential)

DELETE file-record RECORD


[INVALID-KEY imperative-1]
[NOT INVALID-KEY imperative-2]
[END-DELETE]

29
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

30
KSDS CLOSE Statement

• CLOSE statement (sequential)


– list the KSDS that is to be closed

31
KSDS Load Program
in COBOL

• Go over handout

“COBOL JCL
& Source Review for Loading KSDS”

32
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

33
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.

34
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

35
Procedure Division in
KSDS Random Processing

• OPEN statement
• READ statement
• WRITE statement
• REWRITE statement
• DELETE statement
• CLOSE statement

36
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.

37
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

38
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

39
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]

40
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.

41
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
42
KSDS WRITE Statement

• WRITE statement (random)

WRITE record-name [FROM area]


[INVALID-KEY imperative-1]
[NOT INVALID-KEY imperative-2]
[END-WRITE]

43
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

44
KSDS REWRITE statement

• REWRITE statement (random)

REWRITE record-name [FROM area]


[INVALID KEY imperative-1]
[NOT INVALID KEY imperative-2]
[END-REWRITE]

45
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

46
KSDS DELETE Statement

• DELETE statement (random)

DELETE file-record RECORD


[INVALID-KEY imperative-1]
[NOT INVALID-KEY imperative-2]
[END-DELETE]

47
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

48
KSDS CLOSE Statement

• CLOSE statement (random)


– list the KSDS that is to be closed

49
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

50
KSDS Dynamic Processing

• Using dynamic processing effectively is


knowing how to switch from sequential
to random access
– position for sequential retrieval by a
• START or
• random READ
– issue READ using NEXT for sequential
processing

51
KSDS Dynamic Processing

• Note:
– During dynamic processing issuing a
WRITE, REWRITE, or DELETE statement
does not change file position
– Use START or random READ to change
file position

52
File-Control for
Dynamic KSDS

File-Control.
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

53
KSDS READ Statement

• READ statement (dynamic)

READ file-name [NEXT] RECORD [INTO area]


[KEY IS data-name-1]
[INVALID KEY imperative-1]
NOT INVALID KEY imperative-2]
[END-READ]

54
KSDS READ Statement

• READ statement (dynamic)


– NEXT is used in dynamic processing of
KSDS to retrieve records sequentially on
the key
– if NEXT is omitted the records are retrieved
randomly based on the value in the
RECORD KEY field

55