No Slide Title - Columbus State University

advertisement
Chapter 14
How to work with
indexed files
Murach’s Mainframe COBOL
© 2004, Mike Murach & Associates, Inc.
Chapter 14, Slide 1
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.
Murach’s Mainframe COBOL
© 2004, Mike Murach & Associates, Inc.
Chapter 14, Slide 2
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.
Murach’s Mainframe COBOL
© 2004, Mike Murach & Associates, Inc.
Chapter 14, Slide 3
The syntax of the Select statement for indexed
files
SELECT file- name ASSIGN to system- name
ORGANIZATIO N IS INDEXED

 SEQUENTIAL



ACCESS
MODE
IS
RANDOM






 DYNAMIC


RECORD KEY IS data- name- 1
 FILE STATUS IS
data- name- 2 
The syntax for a system name for a VSAM file on
an IBM mainframe
ddname
Murach’s Mainframe COBOL
© 2004, Mike Murach & Associates, Inc.
Chapter 14, Slide 4
A Select statement for a file that’s accessed
sequentially
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 accessed
randomly
SELECT INVMAST
Murach’s Mainframe COBOL
ASSIGN TO INVMAST
ORGANIZATION IS INDEXED
ACCESS IS RANDOM
RECORD KEY IS IM-ITEM-NO.
© 2004, Mike Murach & Associates, Inc.
Chapter 14, Slide 5
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).
Murach’s Mainframe COBOL
© 2004, Mike Murach & Associates, Inc.
Chapter 14, Slide 6
File status categories
First 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 the
condition is defined by the implementor.
Murach’s Mainframe COBOL
© 2004, Mike Murach & Associates, Inc.
Chapter 14, Slide 7
File status codes
Code 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 accessed
sequentially.
22
The program attempted to write or rewrite a record with a duplicate primary key
or 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 defined
boundaries 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 open
mode.
Murach’s Mainframe COBOL
© 2004, Mike Murach & Associates, Inc.
Chapter 14, Slide 8
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, but
the 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 the
record being replaced or is not within the allowed size range of a variablelength record.
46
A read operation was unsuccessful because the end-of-file condition already
occurred or the previous read was unsuccessful.
47
The program attempted a read or start operation on a file that isn’t opened in
input 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 opened
in I-O mode.
Murach’s Mainframe COBOL
© 2004, Mike Murach & Associates, Inc.
Chapter 14, Slide 9
The Open statement for indexed files
 INPUT file - name - 1 ... 
 OUTPUT file - name - 2 ... 


OPEN 
 ...
I
O
file
name
3
...



 EXTEND file - name - 4 ... 

Note
 You can open a file in extend mode only if sequential access
is specified.
Murach’s Mainframe COBOL
© 2004, Mike Murach & Associates, Inc.
Chapter 14, Slide 10
The Read statement for indexed files
The Read statement for sequential access
READ 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 access
READ file-name RECORD [INTO data-name]
[INVALID KEY imperative-statement-1]
[NOT INVALID KEY imperative-statement-2]
[END-READ]
Murach’s Mainframe COBOL
© 2004, Mike Murach & Associates, Inc.
Chapter 14, Slide 11
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, random
access is assumed.
 If a file is accessed sequentially, you can include the At End and
Not At End clauses.
 If the file is accessed randomly, you can use the Invalid Key and
Not Invalid Key clauses.
 The Invalid Key clause is executed whenever an invalid key
condition occurs.
 If an invalid key condition doesn’t occur, the Not Invalid Key
clause is executed.
Murach’s Mainframe COBOL
© 2004, Mike Murach & Associates, Inc.
Chapter 14, Slide 12
The Rewrite statement for indexed files
The Rewrite statement
REWRITE 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 be
rewritten.
 If the file is opened for random or dynamic access, you can use
Rewrite without a prior Read.
 The Invalid Key clause is executed whenever an invalid key
condition occurs.
 If an invalid key condition doesn’t occur, the Not Invalid Key
clause is executed.
Murach’s Mainframe COBOL
© 2004, Mike Murach & Associates, Inc.
Chapter 14, Slide 13
The Write and Close statements for indexed files
The Write statement
WRITE record-name [FROM data-name]
[INVALID KEY imperative-statement-1]
[NOT INVALID KEY imperative-statement-2]
[END-WRITE]
The Close statement
CLOSE 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.
Murach’s Mainframe COBOL
© 2004, Mike Murach & Associates, Inc.
Chapter 14, Slide 14
The syntax of the Start statement


 EQUAL TO











 GREATER THAN









START file- name  KEY IS 
 data- name 
 NOT LESS THAN





NOT







 GREATER THAN OR EQUAL TO 






 

 INVALID KEY imperative- statement- 1 
 NOT INVALID KEY
 END- START 
imperative- statement- 2 
A Start statement that positions the file at the
record with the specified key value
MOVE "1000" TO IM-ITEM-NO.
START INVMAST
KEY IS >= IM-ITEM-NO.
Murach’s Mainframe COBOL
© 2004, Mike Murach & Associates, Inc.
Chapter 14, Slide 15
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 that
record.
 You can use the Start statement with a file that’s opened for
sequential or dynamic access.
Murach’s Mainframe COBOL
© 2004, Mike Murach & Associates, Inc.
Chapter 14, Slide 16
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.
Murach’s Mainframe COBOL
© 2004, Mike Murach & Associates, Inc.
Chapter 14, Slide 17
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 should
not be specified.
 If a file has random or dynamic access, the Delete statement can be
used without reading the record first. In that case, the Invalid Key
clause should be specified.
Murach’s Mainframe COBOL
© 2004, Mike Murach & Associates, Inc.
Chapter 14, Slide 18
The system flowchart for a sequential file creation
program
INVMASTS
(Input)
Create
indexed
file
INVMASTI
(Output)
The record description for the inventory master file
01
INVENTORY-MASTER-RECORD.
05 IM-ITEM-NO
05 IM-DESCRIPTIVE-DATA.
10 IM-ITEM-DESC
10 IM-UNIT-COST
10 IM-UNIT-PRICE
05 IM-INVENTORY-DATA.
10 IM-REORDER-POINT
10 IM-ON-HAND
10 IM-ON-ORDER
Murach’s Mainframe COBOL
PIC X(5).
PIC X(40).
PIC S9(3)V99.
PIC S9(3)V99.
PIC S9(5).
PIC S9(5).
PIC S9(5).
© 2004, Mike Murach & Associates, Inc.
Chapter 14, Slide 19
The structure chart for the sequential file creation
program
000
Create
inventory
file
100
Create
inventory
record
110
Read
sequential
record
Murach’s Mainframe COBOL
120
Write
indexed
record
© 2004, Mike Murach & Associates, Inc.
Chapter 14, Slide 20
Processing specifications for the sequential file
creation 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.
Murach’s Mainframe COBOL
© 2004, Mike Murach & Associates, Inc.
Chapter 14, Slide 21
The sequential file creation program
IDENTIFICATION DIVISION.
PROGRAM-ID. IND1000.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT INVMASTS
SELECT INVMASTI
ASSIGN TO INVMASTS.
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
Murach’s Mainframe COBOL
PIC X(70).
© 2004, Mike Murach & Associates, Inc.
Chapter 14, Slide 22
The sequential file creation program (continued)
FD
INVMASTI.
01
INDEXED-RECORD-AREA.
05 IR-ITEM-NO
05 FILLER
PIC X(5).
PIC X(65).
WORKING-STORAGE SECTION.
01
01
SWITCHES.
05 INVMAST-EOF-SWITCH
88 INVMAST-EOF
INVENTORY-MASTER-RECORD.
05 IM-ITEM-NO
05 IM-DESCRIPTIVE-DATA.
10 IM-ITEM-DESC
10 IM-UNIT-COST
10 IM-UNIT-PRICE
05 IM-INVENTORY-DATA.
10 IM-REORDER-POINT
10 IM-ON-HAND
10 IM-ON-ORDER
Murach’s Mainframe COBOL
PIC X
VALUE "N".
VALUE "Y".
PIC X(5).
PIC X(40).
PIC S9(3)V99.
PIC S9(3)V99.
PIC S9(5).
PIC S9(5).
PIC S9(5).
© 2004, Mike Murach & Associates, Inc.
Chapter 14, Slide 23
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.
Murach’s Mainframe COBOL
© 2004, Mike Murach & Associates, Inc.
Chapter 14, Slide 24
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.
Murach’s Mainframe COBOL
© 2004, Mike Murach & Associates, Inc.
Chapter 14, Slide 25
The system flowchart for a random maintenance
program
INVMAST
(I-O)
MNTTRAN
(Input)
Murach’s Mainframe COBOL
Process
maintenance
transaction
© 2004, Mike Murach & Associates, Inc.
ERRTRAN
(Output)
Chapter 14, Slide 26
The record description for the maintenance
transaction file
01
MAINTENANCE-TRANSACTION.
05 MT-TRANSACTION-CODE
88 DELETE-RECORD
88 ADD-RECORD
88 CHANGE-RECORD
05 MT-MASTER-DATA.
10 MT-ITEM-NO
10 MT-ITEM-DESC
10 MT-UNIT-COST
10 MT-UNIT-PRICE
10 MT-REORDER-POINT
Murach’s Mainframe COBOL
PIC X.
VALUE "1".
VALUE "2".
VALUE "3".
PIC
PIC
PIC
PIC
PIC
© 2004, Mike Murach & Associates, Inc.
X(5).
X(40).
S9(3)V99.
S9(3)V99.
S9(5).
Chapter 14, Slide 27
Processing specifications for the random
maintenance 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 program
should write the transaction to a file of error transactions.
Murach’s Mainframe COBOL
© 2004, Mike Murach & Associates, Inc.
Chapter 14, Slide 28
The structure chart for the random maintenance program
000
Maintain
inventory
file
300
Maintain
inventory
record
310
Read
inventory
transaction
320
Read
inventory
master
330
Delete
inventory
record
340
Add
inventory
record
350
Write
inventory
record
Murach’s Mainframe COBOL
© 2004, Mike Murach & Associates, Inc.
360
Change
inventory
record
380
Write
error
transaction
370
Rewrite
inventory
record
Chapter 14, Slide 29
The modules of the random maintenance
program
 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 master
file 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 change
transaction is unmatched, module 300 performs module 380 to
write an error record.
Murach’s Mainframe COBOL
© 2004, Mike Murach & Associates, Inc.
Chapter 14, Slide 30
The random maintenance program
IDENTIFICATION DIVISION.
PROGRAM-ID.
IND2000.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT MNTTRAN
SELECT INVMAST
SELECT ERRTRAN
ASSIGN TO MNTTRAN.
ASSIGN TO INVMAST
ORGANIZATION IS INDEXED
ACCESS IS RANDOM
RECORD KEY IS IR-ITEM-NO.
ASSIGN TO ERRTRAN
FILE STATUS IS ERRTRAN-FILE-STATUS.
DATA DIVISION.
FILE SECTION.
Murach’s Mainframe COBOL
© 2004, Mike Murach & Associates, Inc.
Chapter 14, Slide 31
The random maintenance program (continued)
FD
01
MNTTRAN.
TRANSACTION-RECORD
PIC X(61).
FD
01
INVMAST.
INVENTORY-RECORD-AREA.
05 IR-ITEM-NO
05 FILLER
PIC X(5).
PIC X(65).
ERRTRAN.
ERROR-TRANSACTION
PIC X(61).
FD
01
WORKING-STORAGE SECTION.
01
01
SWITCHES.
05 TRANSACTION-EOF-SWITCH
88 TRANSACTION-EOF
05 MASTER-FOUND-SWITCH
88 MASTER-FOUND
FILE-STATUS-FIELDS.
05 ERRTRAN-FILE-STATUS
88 ERRTRAN-SUCCESSFUL
Murach’s Mainframe COBOL
PIC X
PIC X
VALUE
VALUE
VALUE
VALUE
"N".
"Y".
"Y".
"Y".
PIC XX.
VALUE "00".
© 2004, Mike Murach & Associates, Inc.
Chapter 14, Slide 32
The random maintenance program (continued)
01
01
MAINTENANCE-TRANSACTION.
05 MT-TRANSACTION-CODE
88 DELETE-RECORD
88 ADD-RECORD
88 CHANGE-RECORD
05 MT-MASTER-DATA.
10 MT-ITEM-NO
10 MT-ITEM-DESC
10 MT-UNIT-COST
10 MT-UNIT-PRICE
10 MT-REORDER-POINT
INVENTORY-MASTER-RECORD.
05 IM-ITEM-NO
05 IM-DESCRIPTIVE-DATA.
10 IM-ITEM-DESC
10 IM-UNIT-COST
10 IM-UNIT-PRICE
05 IM-INVENTORY-DATA.
10 IM-REORDER-POINT
10 IM-ON-HAND
10 IM-ON-ORDER
Murach’s Mainframe COBOL
PIC X.
VALUE "1".
VALUE "2".
VALUE "3".
PIC
PIC
PIC
PIC
PIC
X(5).
X(40).
S9(3)V99.
S9(3)V99.
S9(5).
PIC X(5).
PIC X(40).
PIC S9(3)V99.
PIC S9(3)V99.
PIC S9(5).
PIC S9(5).
PIC S9(5).
© 2004, Mike Murach & Associates, Inc.
Chapter 14, Slide 33
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.
Murach’s Mainframe COBOL
© 2004, Mike Murach & Associates, Inc.
Chapter 14, Slide 34
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.
Murach’s Mainframe COBOL
© 2004, Mike Murach & Associates, Inc.
Chapter 14, Slide 35
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.
Murach’s Mainframe COBOL
© 2004, Mike Murach & Associates, Inc.
Chapter 14, Slide 36
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.
Murach’s Mainframe COBOL
© 2004, Mike Murach & Associates, Inc.
Chapter 14, Slide 37
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.
Murach’s Mainframe COBOL
© 2004, Mike Murach & Associates, Inc.
Chapter 14, Slide 38
The syntax of the Select statement for files with
alternate indexes
SELECT file - name ASSIGN to system - name
ORGANIZATI ON IS INDEXED

 SEQUENTIAL


 ACCESS MODE IS  RANDOM
 DYNAMIC



RECORD KEY IS data - name - 1





 ALTERNATE RECORD KEY IS data - name - 2  WITH
 FILE STATUS IS data - name - 3
DUPLICATES   ...
File status codes for alternate indexes
Code
Meaning
02
A duplicate key condition occurred on an alternate index that allows
duplicates.
22
The program attempted to write or rewrite a record with a duplicate primary
key or an alternate key that doesn’t allow duplicates.
Murach’s Mainframe COBOL
© 2004, Mike Murach & Associates, Inc.
Chapter 14, Slide 39
How to code Select statements for alternate
indexes
 The Alternate Record Key clause names an alternate key field for
a file. This field must be defined in the record description in the
File Section along with the primary key.
 The With Duplicates phrase indicates that duplicate keys are
allowed. Without it, the alternate keys must be unique.
 In most cases, you only need to code the Alternate Record Key
clause for the alternate keys that the program updates or uses to
access records.
 Your program can use file status code 02 to control the
processing of records with duplicate keys.
Murach’s Mainframe COBOL
© 2004, Mike Murach & Associates, Inc.
Chapter 14, Slide 40
The syntax of the Start statement for sequential
access of a file by an alternate index

 EQUAL TO








 GREATER THAN







START file - name  KEY IS 
 data - name
NOT
LESS
THAN



 NOT 





GREATER
THAN
OR
EQUAL
TO



 




 INVALID KEY imperative- statement - 1 
 NOT INVALID KEY imperative- statement - 2 












 END - START 
Description
 You use the Start statement to position a file based on the alternate
key field you specify in the Key clause.
Murach’s Mainframe COBOL
© 2004, Mike Murach & Associates, Inc.
Chapter 14, Slide 41
The syntax of the Read statement for random
access 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.
Murach’s Mainframe COBOL
© 2004, Mike Murach & Associates, Inc.
Chapter 14, Slide 42
Code for processing an indexed file sequentially
by an alternate index
FD
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.
.
OPENITEM
RECORD CONTAINS 69 CHARACTERS.
01
OPEN-ITEM-RECORD.
05 OI-INVOICE-NUMBER
PIC
05 OI-INVOICE-DATE
PIC
05 OI-CUSTOMER-NUMBER
PIC
.
WORKING-STORAGE SECTION.
.
01 FILE-STATUS-FIELDS.
05 OPENITEM-FILE-STATUS
PIC
88 LAST-CUSTOMER-INVOICE
.
Murach’s Mainframe COBOL
X(6).
X(8).
X(6).
XX.
VALUE "00".
© 2004, Mike Murach & Associates, Inc.
Chapter 14, Slide 43
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.
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)
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.
Murach’s Mainframe COBOL
© 2004, Mike Murach & Associates, Inc.
Chapter 14, Slide 45
Code for processing an indexed file randomly by
an alternate index
SELECT EMPMAST
FD
01
ASSIGN TO EMPMAST
ORGANIZATION IS INDEXED
ACCESS IS RANDOM
RECORD KEY IS ER-EMPLOYEE-NUMBER
ALTERNATE RECORD KEY
IS ER-SOCIAL-SECURITY-NUMBER.
.
.
EMPMAST
RECORD CONTAINS 103 CHARACTERS.
EMPLOYEE-RECORD-AREA.
05 ER-EMPLOYEE-NUMBER
05 FILLER
05 ER-SOCIAL-SECURITY-NUMBER
05 FILLER
.
.
Murach’s Mainframe COBOL
PIC
PIC
PIC
PIC
X(5).
X(31).
X(9).
X(58).
© 2004, Mike Murach & Associates, Inc.
Chapter 14, Slide 46
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.
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)
.
.
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.
.
.
Murach’s Mainframe COBOL
© 2004, Mike Murach & Associates, Inc.
Chapter 14, Slide 48
Code for a program that uses dynamic processing
SELECT INVLOC
FD
01
ASSIGN TO INVLOC
ORGANIZATION IS INDEXED
ACCESS IS DYNAMIC
RECORD KEY IS IL-RECORD-KEY.
.
.
INVLOC
RECORD CONTAINS 30 CHARACTERS.
INVENTORY-LOCATION-RECORD.
05 IL-RECORD-KEY.
10 IL-ITEM-NO
10 IL-SEQUENCE-NO
05 IL-LOCATION-DATA.
.
.
Murach’s Mainframe COBOL
PIC X(5).
PIC XX.
© 2004, Mike Murach & Associates, Inc.
Chapter 14, Slide 49
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.
Murach’s Mainframe COBOL
© 2004, Mike Murach & Associates, Inc.
Random
Read
statement
Sequential
Read
statement
Chapter 14, Slide 50
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 file
sequentially.
 You can use dynamic processing to access a file by its primary
key or an alternate key. In either case, you must know the exact
key value of the first record you want to read in a group so you
can read it randomly.
Murach’s Mainframe COBOL
© 2004, Mike Murach & Associates, Inc.
Chapter 14, Slide 51
Code for a program that uses skip-sequential
processing
SELECT INVLOC
FD
01
ASSIGN TO INVLOC
ORGANIZATION IS INDEXED
ACCESS IS SEQUENTIAL
RECORD KEY IS IL-RECORD-KEY.
.
.
INVLOC
RECORD CONTAINS 30 CHARACTERS.
INVENTORY-LOCATION-RECORD.
05 IL-RECORD-KEY.
10 IL-ITEM-NO
10 IL-SEQUENCE-NO
05 IL-LOCATION-DATA.
.
.
Murach’s Mainframe COBOL
PIC X(5).
PIC XX.
© 2004, Mike Murach & Associates, Inc.
Chapter 14, Slide 52
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.
Murach’s Mainframe COBOL
© 2004, Mike Murach & Associates, Inc.
Chapter 14, Slide 53
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 statement
again to position the file at the next group of records to be read.
 You can use skip-sequential processing to access a file by its
primary key or an alternate key. In either case, you can specify
sequential access mode for the file.
 If you specify dynamic access, be sure to include the Next Record
phrase on the Read statement.
Murach’s Mainframe COBOL
© 2004, Mike Murach & Associates, Inc.
Chapter 14, Slide 54
Download