54
Murach’s Mainframe COBOL © 2004, Mike Murach & Associates, I nc. Chapter 14, Slide 1 C hapter14 How to w ork w ith indexed files

Murach’s Mainframe COBOL© 2004, Mike Murach & Associates, Inc.Chapter 14, Slide 1

Embed Size (px)

Citation preview

Page 1: Murach’s Mainframe COBOL© 2004, Mike Murach & Associates, Inc.Chapter 14, Slide 1

Murach’s Mainframe COBOL © 2004, Mike Murach & Associates, Inc. Chapter 14, Slide 1

Chapter 14

How to work withindexed files

Page 2: Murach’s Mainframe COBOL© 2004, Mike Murach & Associates, Inc.Chapter 14, Slide 1

Murach’s Mainframe COBOL © 2004, Mike Murach & Associates, Inc. Chapter 14, Slide 2

Objectives

Applied

Given complete program specifications, develop a program that processes an indexed file sequentially, randomly, or dynamically. The program may use the primary key or an alternate key to access the records in the file.

Knowledge

Name the three access modes you can use for an indexed file.

Describe two ways that a program can detect I/O errors for indexed files.

Describe the operation of the Read, Write, Rewrite, and Delete statements when used with random access of indexed files.

Describe the operation of the Start statement and name the two types of access that it can be used with.

Page 3: Murach’s Mainframe COBOL© 2004, Mike Murach & Associates, Inc.Chapter 14, Slide 1

Murach’s Mainframe COBOL © 2004, Mike Murach & Associates, Inc. Chapter 14, Slide 3

Objectives (continued) Explain how you can use file status codes to determine when

all of the records for a alternate key value have been processed.

In general terms, describe dynamic processing.

In general terms, describe skip-sequential processing.

Page 4: Murach’s Mainframe COBOL© 2004, Mike Murach & Associates, Inc.Chapter 14, Slide 1

Murach’s Mainframe COBOL © 2004, Mike Murach & Associates, Inc. Chapter 14, Slide 4

The syntax of the Select statement for indexed files

The syntax for a system name for a VSAM file on an IBM mainframe

ddname

2-name-data IS STATUS FILE

1-name-data IS KEY RECORD

DYNAMIC

RANDOM

SEQUENTIAL

IS MODE ACCESS

INDEXED IS NOORGANIZATI

name-system to ASSIGN name-file SELECT

Page 5: Murach’s Mainframe COBOL© 2004, Mike Murach & Associates, Inc.Chapter 14, Slide 1

Murach’s Mainframe COBOL © 2004, Mike Murach & Associates, Inc. Chapter 14, Slide 5

A Select statement for a file that’s accessedsequentially

SELECT INVMAST ASSIGN TO INVMAST ORGANIZATION IS INDEXED ACCESS IS SEQUENTIAL RECORD KEY IS IM-ITEM-NO.

A Select statement for a file that’s accessedrandomly

SELECT INVMAST ASSIGN TO INVMAST ORGANIZATION IS INDEXED ACCESS IS RANDOM RECORD KEY IS IM-ITEM-NO.

Page 6: Murach’s Mainframe COBOL© 2004, Mike Murach & Associates, Inc.Chapter 14, Slide 1

Murach’s Mainframe COBOL © 2004, Mike Murach & Associates, Inc. Chapter 14, Slide 6

How to code Select statements for indexed files On an IBM mainframe, the system name consists of a ddname

that’s used in the JCL to identify the VSAM file on disk.

You must code the Organization clause for an indexed file.

You can specify three types of access in the Access clause: Sequential, Random, or Dynamic.

The Record Key clause identifies the primary key field for the file. This field must be included in the record description in the File Section.

The File Status clause names a field that will be updated by the system as each I/O statement for the file is executed. It must be defined in working storage as a two-byte alphanumeric field (XX).

Page 7: Murach’s Mainframe COBOL© 2004, Mike Murach & Associates, Inc.Chapter 14, Slide 1

Murach’s Mainframe COBOL © 2004, Mike Murach & Associates, Inc. Chapter 14, Slide 7

File status categoriesFirst digit Meaning

0 The operation completed successfully.

1 An end-of-file condition occurred.

2 An invalid key condition occurred.

3 A permanent I/O error occurred.

4 A logic error occurred.

9 The operation was unsuccessful, and thecondition is defined by the implementor.

Page 8: Murach’s Mainframe COBOL© 2004, Mike Murach & Associates, Inc.Chapter 14, Slide 1

Murach’s Mainframe COBOL © 2004, Mike Murach & Associates, Inc. Chapter 14, Slide 8

File status codesCode Meaning

00 The I/O operation was successful.

02 A duplicate key condition occurred on an alternate index that allows duplicates.

04 The length of the record that was read doesn’t conform to the file attributes.

10 The end-of-file condition occurred during a read operation (AT END condition).

21 A sequencing error occurred for an indexed file that is being accessedsequentially.

22 The program attempted to write or rewrite a record with a duplicate primary keyor an alternate key that doesn’t allow duplicates.

23 The program attempted to access a record randomly and the record didn’t exist.

24 A write operation attempted to write a record beyond the externally definedboundaries of the file.

30 The I/O operation was unsuccessful. No further information is available.

35 The program attempted to open a nonexistent file in I/O, input, or extend mode.

37 The program attempted to open a file that doesn’t support the specified openmode.

Page 9: Murach’s Mainframe COBOL© 2004, Mike Murach & Associates, Inc.Chapter 14, Slide 1

Murach’s Mainframe COBOL © 2004, Mike Murach & Associates, Inc. Chapter 14, Slide 9

File status codes (continued)39 A conflict occurred between the file attributes and the attributes specified in the

program.

41 The program attempted to open a file that is already open.

42 The program attempted to close a file that is already closed.

43 A delete or rewrite operation was attempted on a file with sequential access, butthe last successful operation on the file was not a Read statement.

44 The program attempted to rewrite a record that is not the same size as therecord being replaced or is not within the allowed size range of a variable-length record.

46 A read operation was unsuccessful because the end-of-file condition alreadyoccurred or the previous read was unsuccessful.

47 The program attempted a read or start operation on a file that isn’t opened ininput or I-O mode.

48 The program attempted a write operation on a file that isn’t opened in output,I-O, or extend mode.

49 The program attempted a delete or rewrite operation on a file that isn’t openedin I-O mode.

Page 10: Murach’s Mainframe COBOL© 2004, Mike Murach & Associates, Inc.Chapter 14, Slide 1

Murach’s Mainframe COBOL © 2004, Mike Murach & Associates, Inc. Chapter 14, Slide 10

The Open statement for indexed files

Note You can open a file in extend mode only if sequential access

is specified.

...

... 4-name-file EXTEND

... 3-name-file O-I

... 2-name-file OUTPUT

... 1-name-file INPUT

OPEN

Page 11: Murach’s Mainframe COBOL© 2004, Mike Murach & Associates, Inc.Chapter 14, Slide 1

Murach’s Mainframe COBOL © 2004, Mike Murach & Associates, Inc. Chapter 14, Slide 11

The Read statement for indexed files

The Read statement for sequential accessREAD file-name [NEXT ] RECORD [INTO data-name] [AT END imperative-statement-1] [NOT AT END imperative-statement-2] [END-READ]

The Read statement for random accessREAD file-name RECORD [INTO data-name] [INVALID KEY imperative-statement-1] [NOT INVALID KEY imperative-statement-2] [END-READ]

Page 12: Murach’s Mainframe COBOL© 2004, Mike Murach & Associates, Inc.Chapter 14, Slide 1

Murach’s Mainframe COBOL © 2004, Mike Murach & Associates, Inc. Chapter 14, Slide 12

How to code the Read statement If a file is opened for dynamic access, you must include the word

NEXT to access the records sequentially. Otherwise, randomaccess is assumed.

If a file is accessed sequentially, you can include the At End andNot At End clauses.

If the file is accessed randomly, you can use the Invalid Key andNot Invalid Key clauses.

The Invalid Key clause is executed whenever an invalid keycondition occurs.

If an invalid key condition doesn’t occur, the Not Invalid Keyclause is executed.

Page 13: Murach’s Mainframe COBOL© 2004, Mike Murach & Associates, Inc.Chapter 14, Slide 1

Murach’s Mainframe COBOL © 2004, Mike Murach & Associates, Inc. Chapter 14, Slide 13

The Rewrite statement for indexed files

The Rewrite statementREWRITE record-name [FROM data-name] [INVALID KEY imperative-statement-1] [NOT INVALID KEY imperative-statement-2] [END-REWRITE]

Description If a file is opened for sequential access, the Rewrite statement can

be used only after executing a Read statement for the record to berewritten.

If the file is opened for random or dynamic access, you can useRewrite without a prior Read.

The Invalid Key clause is executed whenever an invalid keycondition occurs.

If an invalid key condition doesn’t occur, the Not Invalid Keyclause is executed.

Page 14: Murach’s Mainframe COBOL© 2004, Mike Murach & Associates, Inc.Chapter 14, Slide 1

Murach’s Mainframe COBOL © 2004, Mike Murach & Associates, Inc. Chapter 14, Slide 14

The Write and Close statements for indexed files

The Write statementWRITE record-name [FROM data-name] [INVALID KEY imperative-statement-1] [NOT INVALID KEY imperative-statement-2] [END-WRITE]

The Close statementCLOSE file-name-1 ...

Description The Invalid Key clause of the Write statement is executed

whenever an invalid key condition occurs. If an invalid key condition doesn’t occur, the Not Invalid Key

clause is executed.

Page 15: Murach’s Mainframe COBOL© 2004, Mike Murach & Associates, Inc.Chapter 14, Slide 1

Murach’s Mainframe COBOL © 2004, Mike Murach & Associates, Inc. Chapter 14, Slide 15

T h e s y n t a x o f t h e S t a r t s t a t e m e n t

A S t a r t s t a t e m e n t t h a t p o s i t i o n s t h e f i l e a t t h er e c o r d w i t h t h e s p e c i f i e d k e y v a l u e

M O V E " 1 0 0 0 " T O I M - I T E M - N O .S T A R T I N V M A S T K E Y I S > = I M - I T E M - N O .

START-END

2-statement-imperative KEY INVALID NOT

1-statement-imperative KEY INVALID

name-data

TO EQUAL OR THAN GREATER

NOT

THAN LESS NOT

THAN GREATER

TO EQUAL

IS KEY name-file START

Page 16: Murach’s Mainframe COBOL© 2004, Mike Murach & Associates, Inc.Chapter 14, Slide 1

Murach’s Mainframe COBOL © 2004, Mike Murach & Associates, Inc. Chapter 14, Slide 16

How to code the Start statement You can use the Start statement to position an indexed file at a

specific location based on a key value. Once the file is positioned at the correct record, you can use the

Read statement to access records sequentially starting with thatrecord.

You can use the Start statement with a file that’s opened forsequential or dynamic access.

Page 17: Murach’s Mainframe COBOL© 2004, Mike Murach & Associates, Inc.Chapter 14, Slide 1

Murach’s Mainframe COBOL © 2004, Mike Murach & Associates, Inc. Chapter 14, Slide 17

The syntax of the Delete statement DELETE file-name RECORD [INVALID KEY imperative-statement-1] [NOT INVALID KEY imperative-statement-2] [END-DELETE]

A Delete statement that deletes a record using random access

DELETE INVMAST INVALID KEY DISPLAY "INVALID KEY ON DELETE FOR ITEM NUMBER " IM-ITEM-NO.

Page 18: Murach’s Mainframe COBOL© 2004, Mike Murach & Associates, Inc.Chapter 14, Slide 1

Murach’s Mainframe COBOL © 2004, Mike Murach & Associates, Inc. Chapter 14, Slide 18

How to code the Delete statement The Delete statement can be used only for a file that’s opened in

I-O mode. If a file has sequential access, the Delete statement can be used

only after executing a Read statement for the record to be deleted.In that case, the Invalid Key and Not Invalid Key clauses shouldnot be specified.

If a file has random or dynamic access, the Delete statement can beused without reading the record first. In that case, the Invalid Keyclause should be specified.

Page 19: Murach’s Mainframe COBOL© 2004, Mike Murach & Associates, Inc.Chapter 14, Slide 1

Murach’s Mainframe COBOL © 2004, Mike Murach & Associates, Inc. Chapter 14, Slide 19

T h e s y s t e m f l o w c h a r t f o r a s e q u e n t i a l f i l e c r e a t i o np r o g r a m

T h e r e c o r d d e s c r i p t i o n f o r t h e i n v e n t o r y m a s t e r f i l e0 1 I N V E N T O R Y - M A S T E R - R E C O R D . 0 5 I M - I T E M - N O P I C X ( 5 ) . 0 5 I M - D E S C R I P T I V E - D A T A . 1 0 I M - I T E M - D E S C P I C X ( 4 0 ) . 1 0 I M - U N I T - C O S T P I C S 9 ( 3 ) V 9 9 . 1 0 I M - U N I T - P R I C E P I C S 9 ( 3 ) V 9 9 . 0 5 I M - I N V E N T O R Y - D A T A . 1 0 I M - R E O R D E R - P O I N T P I C S 9 ( 5 ) . 1 0 I M - O N - H A N D P I C S 9 ( 5 ) . 1 0 I M - O N - O R D E R P I C S 9 ( 5 ) .

INVMASTS(Input)

INVMASTI(Output)

Createindexed

file

Page 20: Murach’s Mainframe COBOL© 2004, Mike Murach & Associates, Inc.Chapter 14, Slide 1

Murach’s Mainframe COBOL © 2004, Mike Murach & Associates, Inc. Chapter 14, Slide 20

The structure chart for the sequential file creationprogram

Createinventoryfile

Createinventoryrecord

Readsequentialrecord

Writeindexedrecord

120110

100

000

Page 21: Murach’s Mainframe COBOL© 2004, Mike Murach & Associates, Inc.Chapter 14, Slide 1

Murach’s Mainframe COBOL © 2004, Mike Murach & Associates, Inc. Chapter 14, Slide 21

Processing specifications for the sequential filecreation program This program creates an indexed file of inventory master records

from a sequential file of inventory master records. The primary key for the indexed file is the item number. This program assumes that the records in the sequential file are in

sequence by item number. As each record in the sequential file is read, it is written to the

indexed file.

Page 22: Murach’s Mainframe COBOL© 2004, Mike Murach & Associates, Inc.Chapter 14, Slide 1

Murach’s Mainframe COBOL © 2004, Mike Murach & Associates, Inc. Chapter 14, Slide 22

The sequential file creation program IDENTIFICATION DIVISION. PROGRAM-ID. IND1000. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT INVMASTS ASSIGN TO INVMASTS. SELECT INVMASTI ASSIGN TO INVMASTI ORGANIZATION IS INDEXED ACCESS IS SEQUENTIAL RECORD KEY IS IR-ITEM-NO. DATA DIVISION. FILE SECTION. FD INVMASTS. 01 SEQUENTIAL-RECORD-AREA PIC X(70).

Page 23: Murach’s Mainframe COBOL© 2004, Mike Murach & Associates, Inc.Chapter 14, Slide 1

Murach’s Mainframe COBOL © 2004, Mike Murach & Associates, Inc. Chapter 14, Slide 23

The sequential file creation program (continued) FD INVMASTI. 01 INDEXED-RECORD-AREA. 05 IR-ITEM-NO PIC X(5). 05 FILLER PIC X(65). WORKING-STORAGE SECTION. 01 SWITCHES. 05 INVMAST-EOF-SWITCH PIC X VALUE "N". 88 INVMAST-EOF VALUE "Y". 01 INVENTORY-MASTER-RECORD. 05 IM-ITEM-NO PIC X(5). 05 IM-DESCRIPTIVE-DATA. 10 IM-ITEM-DESC PIC X(40). 10 IM-UNIT-COST PIC S9(3)V99. 10 IM-UNIT-PRICE PIC S9(3)V99. 05 IM-INVENTORY-DATA. 10 IM-REORDER-POINT PIC S9(5). 10 IM-ON-HAND PIC S9(5). 10 IM-ON-ORDER PIC S9(5).

Page 24: Murach’s Mainframe COBOL© 2004, Mike Murach & Associates, Inc.Chapter 14, Slide 1

Murach’s Mainframe COBOL © 2004, Mike Murach & Associates, Inc. Chapter 14, Slide 24

The sequential file creation program (continued) PROCEDURE DIVISION. 000-CREATE-INVENTORY-FILE. OPEN INPUT INVMASTS OUTPUT INVMASTI. PERFORM 100-CREATE-INVENTORY-RECORD UNTIL INVMAST-EOF. CLOSE INVMASTS INVMASTI. STOP RUN. 100-CREATE-INVENTORY-RECORD. PERFORM 110-READ-SEQUENTIAL-RECORD. IF NOT INVMAST-EOF PERFORM 120-WRITE-INDEXED-RECORD. 110-READ-SEQUENTIAL-RECORD. READ INVMASTS INTO INVENTORY-MASTER-RECORD AT END SET INVMAST-EOF TO TRUE.

Page 25: Murach’s Mainframe COBOL© 2004, Mike Murach & Associates, Inc.Chapter 14, Slide 1

Murach’s Mainframe COBOL © 2004, Mike Murach & Associates, Inc. Chapter 14, Slide 25

The sequential file creation program (continued) 120-WRITE-INDEXED-RECORD. WRITE INDEXED-RECORD-AREA FROM INVENTORY-MASTER-RECORD INVALID KEY DISPLAY "WRITE ERROR ON INVMASTI FOR ITEM NUMBER " IR-ITEM-NO SET INVMAST-EOF TO TRUE.

Page 26: Murach’s Mainframe COBOL© 2004, Mike Murach & Associates, Inc.Chapter 14, Slide 1

Murach’s Mainframe COBOL © 2004, Mike Murach & Associates, Inc. Chapter 14, Slide 26

The system flowchart for a random maintenanceprogram

MNTTRAN(Input)

ERRTRAN(Output)

Processmaintenancetransaction

INVMAST(I-O)

Page 27: Murach’s Mainframe COBOL© 2004, Mike Murach & Associates, Inc.Chapter 14, Slide 1

Murach’s Mainframe COBOL © 2004, Mike Murach & Associates, Inc. Chapter 14, Slide 27

The record description for the maintenancetransaction file

01 MAINTENANCE-TRANSACTION. 05 MT-TRANSACTION-CODE PIC X. 88 DELETE-RECORD VALUE "1". 88 ADD-RECORD VALUE "2". 88 CHANGE-RECORD VALUE "3". 05 MT-MASTER-DATA. 10 MT-ITEM-NO PIC X(5). 10 MT-ITEM-DESC PIC X(40). 10 MT-UNIT-COST PIC S9(3)V99. 10 MT-UNIT-PRICE PIC S9(3)V99. 10 MT-REORDER-POINT PIC S9(5).

Page 28: Murach’s Mainframe COBOL© 2004, Mike Murach & Associates, Inc.Chapter 14, Slide 1

Murach’s Mainframe COBOL © 2004, Mike Murach & Associates, Inc. Chapter 14, Slide 28

Processing specifications for the randommaintenance program This program adds, changes, and deletes records in an inventory

master file based on the data in a file of maintenance transactions. The inventory master file is indexed by item number and should be

updated randomly. The program assumes the transactions have been edited and are in

sequence by transaction code within item number. If there’s a change or delete transaction for a record that doesn’t

exist or an add transaction for an existing record, the programshould write the transaction to a file of error transactions.

Page 29: Murach’s Mainframe COBOL© 2004, Mike Murach & Associates, Inc.Chapter 14, Slide 1

Murach’s Mainframe COBOL © 2004, Mike Murach & Associates, Inc. Chapter 14, Slide 29

The structure chart for the random maintenance program

Readinventorytransaction

Readinventorymaster

Deleteinventoryrecord

Addinventoryrecord

Changeinventoryrecord

Writeerrortransaction

Writeinventoryrecord

Rewriteinventoryrecord

Maintaininventoryrecord

Maintaininventoryfile

000

300

310 320 330 340 360 380

350 370

Page 30: Murach’s Mainframe COBOL© 2004, Mike Murach & Associates, Inc.Chapter 14, Slide 1

Murach’s Mainframe COBOL © 2004, Mike Murach & Associates, Inc. Chapter 14, Slide 30

The modules of the random maintenanceprogram Module 000 performs module 300 until all of the records in the

transaction file have been processed. Module 300 performs module 310 to read a record from the

transaction file. Then, it performs module 320 to read the masterfile randomly to see if a record exists with the same item number.

If it’s a deletion transaction and a matching master is found,module 300 performs module 330 to delete the master record.

If it’s an addition transaction and a matching master isn’t found,module 300 performs module 340 to add the new master record.

If it’s a change transaction and a matching master is found,module 300 performs module 360 to change the master record.

If an addition transaction is matched, or if a deletion or changetransaction is unmatched, module 300 performs module 380 towrite an error record.

Page 31: Murach’s Mainframe COBOL© 2004, Mike Murach & Associates, Inc.Chapter 14, Slide 1

Murach’s Mainframe COBOL © 2004, Mike Murach & Associates, Inc. Chapter 14, Slide 31

The random maintenance program IDENTIFICATION DIVISION. PROGRAM-ID. IND2000. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT MNTTRAN ASSIGN TO MNTTRAN. SELECT INVMAST ASSIGN TO INVMAST ORGANIZATION IS INDEXED ACCESS IS RANDOM RECORD KEY IS IR-ITEM-NO. SELECT ERRTRAN ASSIGN TO ERRTRAN FILE STATUS IS ERRTRAN-FILE-STATUS. DATA DIVISION. FILE SECTION.

Page 32: Murach’s Mainframe COBOL© 2004, Mike Murach & Associates, Inc.Chapter 14, Slide 1

Murach’s Mainframe COBOL © 2004, Mike Murach & Associates, Inc. Chapter 14, Slide 32

The random maintenance program (continued) FD MNTTRAN. 01 TRANSACTION-RECORD PIC X(61). FD INVMAST. 01 INVENTORY-RECORD-AREA. 05 IR-ITEM-NO PIC X(5). 05 FILLER PIC X(65). FD ERRTRAN. 01 ERROR-TRANSACTION PIC X(61). WORKING-STORAGE SECTION. 01 SWITCHES. 05 TRANSACTION-EOF-SWITCH PIC X VALUE "N". 88 TRANSACTION-EOF VALUE "Y". 05 MASTER-FOUND-SWITCH PIC X VALUE "Y". 88 MASTER-FOUND VALUE "Y". 01 FILE-STATUS-FIELDS. 05 ERRTRAN-FILE-STATUS PIC XX. 88 ERRTRAN-SUCCESSFUL VALUE "00".

Page 33: Murach’s Mainframe COBOL© 2004, Mike Murach & Associates, Inc.Chapter 14, Slide 1

Murach’s Mainframe COBOL © 2004, Mike Murach & Associates, Inc. Chapter 14, Slide 33

The random maintenance program (continued) 01 MAINTENANCE-TRANSACTION. 05 MT-TRANSACTION-CODE PIC X. 88 DELETE-RECORD VALUE "1". 88 ADD-RECORD VALUE "2". 88 CHANGE-RECORD VALUE "3". 05 MT-MASTER-DATA. 10 MT-ITEM-NO PIC X(5). 10 MT-ITEM-DESC PIC X(40). 10 MT-UNIT-COST PIC S9(3)V99. 10 MT-UNIT-PRICE PIC S9(3)V99. 10 MT-REORDER-POINT PIC S9(5). 01 INVENTORY-MASTER-RECORD. 05 IM-ITEM-NO PIC X(5). 05 IM-DESCRIPTIVE-DATA. 10 IM-ITEM-DESC PIC X(40). 10 IM-UNIT-COST PIC S9(3)V99. 10 IM-UNIT-PRICE PIC S9(3)V99. 05 IM-INVENTORY-DATA. 10 IM-REORDER-POINT PIC S9(5). 10 IM-ON-HAND PIC S9(5). 10 IM-ON-ORDER PIC S9(5).

Page 34: Murach’s Mainframe COBOL© 2004, Mike Murach & Associates, Inc.Chapter 14, Slide 1

Murach’s Mainframe COBOL © 2004, Mike Murach & Associates, Inc. Chapter 14, Slide 34

The random maintenance program (continued) PROCEDURE DIVISION. 000-MAINTAIN-INVENTORY-FILE. OPEN INPUT MNTTRAN I-O INVMAST OUTPUT ERRTRAN. PERFORM 300-MAINTAIN-INVENTORY-RECORD UNTIL TRANSACTION-EOF. CLOSE MNTTRAN INVMAST ERRTRAN. STOP RUN.

Page 35: Murach’s Mainframe COBOL© 2004, Mike Murach & Associates, Inc.Chapter 14, Slide 1

Murach’s Mainframe COBOL © 2004, Mike Murach & Associates, Inc. Chapter 14, Slide 35

The random maintenance program (continued) 300-MAINTAIN-INVENTORY-RECORD. PERFORM 310-READ-INVENTORY-TRANSACTION. IF NOT TRANSACTION-EOF PERFORM 320-READ-INVENTORY-MASTER IF DELETE-RECORD IF MASTER-FOUND PERFORM 330-DELETE-INVENTORY-RECORD ELSE PERFORM 380-WRITE-ERROR-TRANSACTION ELSE IF ADD-RECORD IF MASTER-FOUND PERFORM 380-WRITE-ERROR-TRANSACTION ELSE PERFORM 340-ADD-INVENTORY-RECORD ELSE IF CHANGE-RECORD IF MASTER-FOUND PERFORM 360-CHANGE-INVENTORY-RECORD ELSE PERFORM 380-WRITE-ERROR-TRANSACTION.

Page 36: Murach’s Mainframe COBOL© 2004, Mike Murach & Associates, Inc.Chapter 14, Slide 1

Murach’s Mainframe COBOL © 2004, Mike Murach & Associates, Inc. Chapter 14, Slide 36

The random maintenance program (continued) 310-READ-INVENTORY-TRANSACTION. READ MNTTRAN INTO MAINTENANCE-TRANSACTION AT END SET TRANSACTION-EOF TO TRUE. 320-READ-INVENTORY-MASTER. MOVE MT-ITEM-NO TO IR-ITEM-NO. READ INVMAST INTO INVENTORY-MASTER-RECORD INVALID KEY MOVE "N" TO MASTER-FOUND-SWITCH NOT INVALID KEY SET MASTER-FOUND TO TRUE. 330-DELETE-INVENTORY-RECORD. DELETE INVMAST.

Page 37: Murach’s Mainframe COBOL© 2004, Mike Murach & Associates, Inc.Chapter 14, Slide 1

Murach’s Mainframe COBOL © 2004, Mike Murach & Associates, Inc. Chapter 14, Slide 37

The random maintenance program (continued) 340-ADD-INVENTORY-RECORD. MOVE MT-ITEM-NO TO IM-ITEM-NO. MOVE MT-ITEM-DESC TO IM-ITEM-DESC. MOVE MT-UNIT-COST TO IM-UNIT-COST. MOVE MT-UNIT-PRICE TO IM-UNIT-PRICE. MOVE MT-REORDER-POINT TO IM-REORDER-POINT. MOVE ZERO TO IM-ON-HAND. MOVE ZERO TO IM-ON-ORDER. PERFORM 350-WRITE-INVENTORY-RECORD. 350-WRITE-INVENTORY-RECORD. WRITE INVENTORY-RECORD-AREA FROM INVENTORY-MASTER-RECORD INVALID KEY DISPLAY "WRITE ERROR ON INVMAST FOR ITEM NUMBER " IR-ITEM-NO SET TRANSACTION-EOF TO TRUE.

Page 38: Murach’s Mainframe COBOL© 2004, Mike Murach & Associates, Inc.Chapter 14, Slide 1

Murach’s Mainframe COBOL © 2004, Mike Murach & Associates, Inc. Chapter 14, Slide 38

The random maintenance program (continued) 360-CHANGE-INVENTORY-RECORD. IF MT-ITEM-DESC NOT = SPACE MOVE MT-ITEM-DESC TO IM-ITEM-DESC. IF MT-UNIT-COST NOT = ZERO MOVE MT-UNIT-COST TO IM-UNIT-COST. IF MT-UNIT-PRICE NOT = ZERO MOVE MT-UNIT-PRICE TO IM-UNIT-PRICE. IF MT-REORDER-POINT NOT = ZERO MOVE MT-REORDER-POINT TO IM-REORDER-POINT. PERFORM 370-REWRITE-INVENTORY-RECORD. 370-REWRITE-INVENTORY-RECORD. REWRITE INVENTORY-RECORD-AREA FROM INVENTORY-MASTER-RECORD. 380-WRITE-ERROR-TRANSACTION. WRITE ERROR-TRANSACTION FROM MAINTENANCE-TRANSACTION. IF NOT ERRTRAN-SUCCESSFUL DISPLAY "WRITE ERROR ON ERRTRAN FOR ITEM NUMBER " MT-ITEM-NO DISPLAY "FILE STATUS CODE IS " ERRTRAN-FILE-STATUS SET TRANSACTION-EOF TO TRUE.

Page 39: Murach’s Mainframe COBOL© 2004, Mike Murach & Associates, Inc.Chapter 14, Slide 1

Murach’s Mainframe COBOL © 2004, Mike Murach & Associates, Inc. Chapter 14, Slide 39

T h e s y n t a x o f t h e S e l e c t s t a t e m e n t f o r f i l e s w i t ha l t e r n a t e i n d e x e s

F i l e s t a t u s c o d e s f o r a l t e r n a t e i n d e x e sC o d e M e a n i n g

0 2 A d u p l i c a t e k e y c o n d i t i o n o c c u r r e d o n a n a l t e r n a t e i n d e x t h a t a l l o w sd u p l i c a t e s .

2 2 T h e p r o g r a m a t t e m p t e d t o w r i t e o r r e w r i t e a r e c o r d w i t h a d u p l i c a t e p r i m a r yk e y o r a n a l t e r n a t e k e y t h a t d o e s n ’ t a l l o w d u p l i c a t e s .

3-name-data IS STATUS FILE

... DUPLICATES WITH 2-name-data IS KEY RECORD ALTERNATE

1-name-data IS KEY RECORD

DYNAMIC

RANDOM

SEQUENTIAL

IS MODE ACCESS

INDEXED IS ONORGANIZATI

name-system to ASSIGN name-file SELECT

Page 40: Murach’s Mainframe COBOL© 2004, Mike Murach & Associates, Inc.Chapter 14, Slide 1

Murach’s Mainframe COBOL © 2004, Mike Murach & Associates, Inc. Chapter 14, Slide 40

How to code Select statements for alternateindexes The Alternate Record Key clause names an alternate key field for

a file. This field must be defined in the record description in theFile Section along with the primary key.

The With Duplicates phrase indicates that duplicate keys areallowed. Without it, the alternate keys must be unique.

In most cases, you only need to code the Alternate Record Keyclause for the alternate keys that the program updates or uses toaccess records.

Your program can use file status code 02 to control theprocessing of records with duplicate keys.

Page 41: Murach’s Mainframe COBOL© 2004, Mike Murach & Associates, Inc.Chapter 14, Slide 1

Murach’s Mainframe COBOL © 2004, Mike Murach & Associates, Inc. Chapter 14, Slide 41

T h e s y n t a x o f t h e S t a r t s t a t e m e n t f o r s e q u e n t i a la c c e s s o f a f i l e b y a n a l t e r n a t e i n d e x

D e s c r i p t i o n Y o u u s e t h e S t a r t s t a t e m e n t t o p o s i t i o n a f i l e b a s e d o n t h e a l t e r n a t e

k e y f i e l d y o u s p e c i f y i n t h e K e y c l a u s e .

START-END

2-statement-imperative KEY INVALID NOT

1-statement-imperative KEY INVALID

name-data

TO EQUAL OR THAN GREATER

NOT

THAN LESS NOT

THAN GREATER

TO EQUAL

IS KEY name-file START

Page 42: Murach’s Mainframe COBOL© 2004, Mike Murach & Associates, Inc.Chapter 14, Slide 1

Murach’s Mainframe COBOL © 2004, Mike Murach & Associates, Inc. Chapter 14, Slide 42

The syntax of the Read statement for randomaccess of a file by an alternate index

READ file-name RECORD [INTO identifier] KEY is data-name [INVALID KEY imperative-statement-1] [NOT INVALID KEY imperative-statement-2] [END-READ]

Description After you position a file using the Start statement, you can use the

Read statement to access records sequentially by the alternate key. To read a record randomly based on an alternate key, you name

the alternate key field in the Key clause.

Page 43: Murach’s Mainframe COBOL© 2004, Mike Murach & Associates, Inc.Chapter 14, Slide 1

Murach’s Mainframe COBOL © 2004, Mike Murach & Associates, Inc. Chapter 14, Slide 43

Code for processing an indexed file sequentially by an alternate index SELECT OPENITEM ASSIGN TO OPENITEM ORGANIZATION IS INDEXED ACCESS IS SEQUENTIAL RECORD KEY IS OI-INVOICE-NUMBER ALTERNATE RECORD KEY IS OI-CUSTOMER-NUMBER WITH DUPLICATES FILE STATUS IS OPENITEM-FILE-STATUS. . FD OPENITEM RECORD CONTAINS 69 CHARACTERS. 01 OPEN-ITEM-RECORD. 05 OI-INVOICE-NUMBER PIC X(6). 05 OI-INVOICE-DATE PIC X(8). 05 OI-CUSTOMER-NUMBER PIC X(6). . WORKING-STORAGE SECTION. . 01 FILE-STATUS-FIELDS. 05 OPENITEM-FILE-STATUS PIC XX. 88 LAST-CUSTOMER-INVOICE VALUE "00". .

Page 44: Murach’s Mainframe COBOL© 2004, Mike Murach & Associates, Inc.Chapter 14, Slide 1

Murach’s Mainframe COBOL © 2004, Mike Murach & Associates, Inc. Chapter 14, Slide 44

Code for processing an indexed file sequentially by an alternate index (continued) PROCEDURE DIVISION. 000-PREPARE-OPEN-ITEM-LISTING. OPEN INPUT OPENITEM OUTPUT OILIST. PERFORM 100-START-OPEN-ITEM-FILE. IF NOT OPENITEM-EOF PERFORM 300-PREPARE-OPEN-ITEM-LINES UNTIL OPENITEM-EOF . 100-START-OPEN-ITEM-FILE. MOVE LOW-VALUE TO OI-CUSTOMER-NUMBER. START OPENITEM KEY IS >= OI-CUSTOMER-NUMBER INVALID KEY SET OPENITEM-EOF TO TRUE.

Page 45: Murach’s Mainframe COBOL© 2004, Mike Murach & Associates, Inc.Chapter 14, Slide 1

Murach’s Mainframe COBOL © 2004, Mike Murach & Associates, Inc. Chapter 14, Slide 45

Code for processing an indexed file sequentially by an alternate index (continued) 300-PREPARE-OPEN-ITEM-LINES. PERFORM 310-READ-OPEN-ITEM-RECORD. IF NOT OPENITEM-EOF . IF LAST-CUSTOMER-INVOICE PERFORM 370-PRINT-TOTAL-LINE . 310-READ-OPEN-ITEM-RECORD. READ OPENITEM AT END SET OPENITEM-EOF TO TRUE.

Page 46: Murach’s Mainframe COBOL© 2004, Mike Murach & Associates, Inc.Chapter 14, Slide 1

Murach’s Mainframe COBOL © 2004, Mike Murach & Associates, Inc. Chapter 14, Slide 46

Code for processing an indexed file randomly by an alternate index SELECT EMPMAST ASSIGN TO EMPMAST ORGANIZATION IS INDEXED ACCESS IS RANDOM RECORD KEY IS ER-EMPLOYEE-NUMBER ALTERNATE RECORD KEY IS ER-SOCIAL-SECURITY-NUMBER. . . FD EMPMAST RECORD CONTAINS 103 CHARACTERS. 01 EMPLOYEE-RECORD-AREA. 05 ER-EMPLOYEE-NUMBER PIC X(5). 05 FILLER PIC X(31). 05 ER-SOCIAL-SECURITY-NUMBER PIC X(9). 05 FILLER PIC X(58). . .

Page 47: Murach’s Mainframe COBOL© 2004, Mike Murach & Associates, Inc.Chapter 14, Slide 1

Murach’s Mainframe COBOL © 2004, Mike Murach & Associates, Inc. Chapter 14, Slide 47

Code for processing an indexed file randomly by an alternate index (continued) PROCEDURE DIVISION. . 300-UPDATE-EMPLOYEE-RECORD. PERFORM 310-READ-EMPLOYEE-TRANSACTION. IF NOT TRANSACTION-EOF PERFORM 320-READ-EMPLOYEE-MASTER IF MASTER-FOUND PERFORM 330-UPDATE-EMPLOYEE-MASTER PERFORM 340-REWRITE-EMPLOYEE-MASTER ELSE PERFORM 350-WRITE-ERROR-TRANSACTION. . 320-READ-EMPLOYEE-MASTER. MOVE EMT-SOCIAL-SECURITY-NUMBER TO ER-SOCIAL-SECURITY-NUMBER. READ EMPMAST INTO EMPLOYEE-MASTER-RECORD KEY IS ER-SOCIAL-SECURITY-NUMBER INVALID KEY MOVE "N" TO MASTER-FOUND-SWITCH NOT INVALID KEY SET MASTER-FOUND TO TRUE.

Page 48: Murach’s Mainframe COBOL© 2004, Mike Murach & Associates, Inc.Chapter 14, Slide 1

Murach’s Mainframe COBOL © 2004, Mike Murach & Associates, Inc. Chapter 14, Slide 48

Code for processing an indexed file randomly by an alternate index (continued) . . 340-REWRITE-EMPLOYEE-MASTER. REWRITE EMPLOYEE-RECORD-AREA FROM EMPLOYEE-MASTER-RECORD INVALID KEY DISPLAY "REWRITE ERROR ON EMPMAST FOR SSN = " ER-SOCIAL-SECURITY-NUMBER SET TRANSACTION-EOF TO TRUE. . .

Page 49: Murach’s Mainframe COBOL© 2004, Mike Murach & Associates, Inc.Chapter 14, Slide 1

Murach’s Mainframe COBOL © 2004, Mike Murach & Associates, Inc. Chapter 14, Slide 49

Code for a program that uses dynamic processing SELECT INVLOC ASSIGN TO INVLOC ORGANIZATION IS INDEXED ACCESS IS DYNAMIC RECORD KEY IS IL-RECORD-KEY. . . FD INVLOC RECORD CONTAINS 30 CHARACTERS. 01 INVENTORY-LOCATION-RECORD. 05 IL-RECORD-KEY. 10 IL-ITEM-NO PIC X(5). 10 IL-SEQUENCE-NO PIC XX. 05 IL-LOCATION-DATA. . .

Page 50: Murach’s Mainframe COBOL© 2004, Mike Murach & Associates, Inc.Chapter 14, Slide 1

Murach’s Mainframe COBOL © 2004, Mike Murach & Associates, Inc. Chapter 14, Slide 50

Code for a program that uses dynamic processing (continued) PROCEDURE DIVISION. . . 330-READ-FIRST-LOCATION-RECORD. MOVE ITEM-NUMBER TO IL-ITEM-NO. MOVE "01" TO IL-SEQUENCE-NO. READ INVLOC INVALID KEY MOVE "N" TO LOCATION-FOUND-SWITCH. . . 350-READ-NEXT-LOCATION-RECORD. READ INVLOC NEXT RECORD AT END MOVE "N" TO LOCATION-FOUND-SWITCH. IF IL-ITEM-NO NOT = ITEM-NUMBER MOVE "N" TO LOCATION-FOUND-SWITCH.

Random Read statement

Sequential Read statement

Page 51: Murach’s Mainframe COBOL© 2004, Mike Murach & Associates, Inc.Chapter 14, Slide 1

Murach’s Mainframe COBOL © 2004, Mike Murach & Associates, Inc. Chapter 14, Slide 51

How to use dynamic processing When you specify dynamic access mode for a file, you can read

the file both randomly and sequentially. Usually, you’ll use random access to read the first record in a

group of records, and you’ll use sequential access to read the rest. When you process a file dynamically, you must include the Next

Record phrase on the Read statement that reads the filesequentially.

You can use dynamic processing to access a file by its primarykey or an alternate key. In either case, you must know the exactkey value of the first record you want to read in a group so youcan read it randomly.

Page 52: Murach’s Mainframe COBOL© 2004, Mike Murach & Associates, Inc.Chapter 14, Slide 1

Murach’s Mainframe COBOL © 2004, Mike Murach & Associates, Inc. Chapter 14, Slide 52

Code for a program that uses skip-sequential processing SELECT INVLOC ASSIGN TO INVLOC ORGANIZATION IS INDEXED ACCESS IS SEQUENTIAL RECORD KEY IS IL-RECORD-KEY. . . FD INVLOC RECORD CONTAINS 30 CHARACTERS. 01 INVENTORY-LOCATION-RECORD. 05 IL-RECORD-KEY. 10 IL-ITEM-NO PIC X(5). 10 IL-SEQUENCE-NO PIC XX. 05 IL-LOCATION-DATA. . .

Page 53: Murach’s Mainframe COBOL© 2004, Mike Murach & Associates, Inc.Chapter 14, Slide 1

Murach’s Mainframe COBOL © 2004, Mike Murach & Associates, Inc. Chapter 14, Slide 53

Code for a program that uses skip-sequential processing (continued) PROCEDURE DIVISION. . . 320-START-LOCATION-FILE. MOVE ITEM-NUMBER TO IL-ITEM-NO. MOVE LOW-VALUE TO IL-SEQUENCE-NO. START INVLOC KEY IS > IL-RECORD-KEY INVALID KEY MOVE "N" TO LOCATION-FOUND-SWITCH. . . 340-READ-LOCATION-RECORD. READ INVLOC AT END MOVE "N" TO LOCATION-FOUND-SWITCH. IF IL-ITEM-NO NOT = ITEM-NUMBER MOVE "N" TO LOCATION-FOUND-SWITCH.

Page 54: Murach’s Mainframe COBOL© 2004, Mike Murach & Associates, Inc.Chapter 14, Slide 1

Murach’s Mainframe COBOL © 2004, Mike Murach & Associates, Inc. Chapter 14, Slide 54

How to use skip-sequential processing To use skip-sequential processing, you use a Start statement to

position the file at the first record to be read. After you position the file, you use a Read statement to read the

records that follow in sequence. Usually, you’ll continue reading records until the key value or part

of the key value changes. Then, you can use the Start statementagain to position the file at the next group of records to be read.

You can use skip-sequential processing to access a file by itsprimary key or an alternate key. In either case, you can specifysequential access mode for the file.

If you specify dynamic access, be sure to include the Next Recordphrase on the Read statement.