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

03 - VSAM KSDS and COBOL

Embed Size (px)

Citation preview

Page 1: 03 - VSAM KSDS and COBOL

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

Page 2: 03 - VSAM KSDS and COBOL

2

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

Page 3: 03 - VSAM KSDS and COBOL

3

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

Page 4: 03 - VSAM KSDS and COBOL

4

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.

Page 5: 03 - VSAM KSDS and COBOL

5

File-Control for Sequential KSDS

• File-Control.SELECT file-name

where file-name is the name used for the file throughout the

program

Page 6: 03 - VSAM KSDS and COBOL

6

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

Page 7: 03 - VSAM KSDS and COBOL

7

File-Control for Sequential KSDS

• File-Control.ORGANIZATION is INDEXED

where INDEXED specifies that this file is a KSDS

Page 8: 03 - VSAM KSDS and COBOL

8

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

Page 9: 03 - VSAM KSDS and COBOL

9

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

Page 10: 03 - VSAM KSDS and COBOL

10

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

Page 11: 03 - VSAM KSDS and COBOL

11

Common File Status for KSDS

Code Meaning00 Successful completion

10 End of file reached

21 Sequence error

22 Duplicate key

23 Record not found

24 No more space

Page 12: 03 - VSAM KSDS and COBOL

12

FD Statement in KSDS

FD KSDS-file

LABEL RECORDS ARE [OMITTED

or STANDARD].

Treated as a comment but is required

Page 13: 03 - VSAM KSDS and COBOL

13

Procedure Division in KSDS Sequential Processing

• OPEN statement

• START statement

• READ statement

• WRITE statement

• REWRITE statement

• DELETE statement

• CLOSE statement

Page 14: 03 - VSAM KSDS and COBOL

14

KSDS OPEN Statement

• OPEN statement (sequentially)

• Must open all files– Loading sequentially

• OPEN OUTPUT ksds-file.

– Retrieving sequentially• OPEN INPUT ksds-file.

Page 15: 03 - VSAM KSDS and COBOL

15

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

Page 16: 03 - VSAM KSDS and COBOL

16

KSDS START Statement

• START statementSTART vsam-file

[KEY IS {EQUAL TO=GREATER THAN>NOT LESS THANNOT <GREATER THAN OR EQUAL TO>= }

record-key][INVALID KEY imperative-1]

[NOT INVALID KEY imperative-2][END-START]

Page 17: 03 - VSAM KSDS and COBOL

17

KSDS READ Statement

• READ statement (sequential)– To retrieve records in key sequence, open

KSDS for either• INPUT or• I-O

Page 18: 03 - VSAM KSDS and COBOL

18

KSDS READ Statement

• READ statement (sequential)

READ file-name RECORD [INTO area]

[AT END imperative-1]

NOT AT END imperative-2]

[END-READ]

Page 19: 03 - VSAM KSDS and COBOL

19

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

Page 20: 03 - VSAM KSDS and COBOL

20

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.

Page 21: 03 - VSAM KSDS and COBOL

21

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)

Page 22: 03 - VSAM KSDS and COBOL

22

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

Page 23: 03 - VSAM KSDS and COBOL

23

KSDS WRITE Statement

• WRITE statement (sequential)

WRITE record-name [FROM area]

[INVALID-KEY imperative-1]

[NOT INVALID-KEY imperative-2]

[END-WRITE]

Page 24: 03 - VSAM KSDS and COBOL

24

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

Page 25: 03 - VSAM KSDS and COBOL

25

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

Page 26: 03 - VSAM KSDS and COBOL

26

KSDS REWRITE Statement

• REWRITE statement (sequential)

REWRITE record-name [FROM area]

[INVALID KEY imperative-1]

[NOT INVALID KEY imperative-2]

[END-REWRITE]

Page 27: 03 - VSAM KSDS and COBOL

27

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

Page 28: 03 - VSAM KSDS and COBOL

28

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

Page 29: 03 - VSAM KSDS and COBOL

29

KSDS DELETE Statement

• DELETE statement (sequential)

DELETE file-record RECORD

[INVALID-KEY imperative-1]

[NOT INVALID-KEY imperative-2]

[END-DELETE]

Page 30: 03 - VSAM KSDS and COBOL

30

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

Page 31: 03 - VSAM KSDS and COBOL

31

KSDS CLOSE Statement

• CLOSE statement (sequential)– list the KSDS that is to be closed

Page 32: 03 - VSAM KSDS and COBOL

32

KSDS Load Program in COBOL

• Go over handout

“COBOL JCL

& Source Review for Loading KSDS”

Page 33: 03 - VSAM KSDS and COBOL

33

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

Page 34: 03 - VSAM KSDS and COBOL

34

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.

Page 35: 03 - VSAM KSDS and COBOL

35

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

Page 36: 03 - VSAM KSDS and COBOL

36

Procedure Division in KSDS Random Processing

• OPEN statement

• READ statement

• WRITE statement

• REWRITE statement

• DELETE statement

• CLOSE statement

Page 37: 03 - VSAM KSDS and COBOL

37

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.

Page 38: 03 - VSAM KSDS and COBOL

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

Page 39: 03 - VSAM KSDS and COBOL

39

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

Page 40: 03 - VSAM KSDS and COBOL

40

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]

Page 41: 03 - VSAM KSDS and COBOL

41

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.

Page 42: 03 - VSAM KSDS and COBOL

42

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

Page 43: 03 - VSAM KSDS and COBOL

43

KSDS WRITE Statement

• WRITE statement (random)

WRITE record-name [FROM area]

[INVALID-KEY imperative-1]

[NOT INVALID-KEY imperative-2]

[END-WRITE]

Page 44: 03 - VSAM KSDS and COBOL

44

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

Page 45: 03 - VSAM KSDS and COBOL

45

KSDS REWRITE statement

• REWRITE statement (random)

REWRITE record-name [FROM area]

[INVALID KEY imperative-1]

[NOT INVALID KEY imperative-2]

[END-REWRITE]

Page 46: 03 - VSAM KSDS and COBOL

46

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

Page 47: 03 - VSAM KSDS and COBOL

47

KSDS DELETE Statement

• DELETE statement (random)

DELETE file-record RECORD

[INVALID-KEY imperative-1]

[NOT INVALID-KEY imperative-2]

[END-DELETE]

Page 48: 03 - VSAM KSDS and COBOL

48

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

Page 49: 03 - VSAM KSDS and COBOL

49

KSDS CLOSE Statement

• CLOSE statement (random)– list the KSDS that is to be closed

Page 50: 03 - VSAM KSDS and COBOL

50

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

Page 51: 03 - VSAM KSDS and COBOL

51

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

Page 52: 03 - VSAM KSDS and COBOL

52

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

Page 53: 03 - VSAM KSDS and COBOL

53

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

Page 54: 03 - VSAM KSDS and COBOL

54

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]

Page 55: 03 - VSAM KSDS and COBOL

55

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