Directory UMM :wiley:Public:college:cobol:

Chapter 16: Indexed File Processing ● 661

COBOL 74 users
should substitute
nested I Fs

A DDITIONAL O PTIONS

STOP RUN.
200- CALC- RTN.
MOVE TRANS- KEY TO MASTER- KEY.
EVALUATE TRUE
WHEN CODE- X = 1
PERFORM 300- NEW- ACCT
WHEN CODE- X = 2
PERFORM 400- UPDATE- RTN
WHEN CODE- X = 3
PERFORM 500- DELETE- RTN
WHEN OTHER
DI SPLAY ’ ERROR’
END- EVALUATE.

READ TRANS
AT END MOVE ’ NO ’ TO ARE- THERE- MORE- RECORDS
END- READ.
300- NEW- ACCT.
MOVE TRANS- DATA TO MASTER- DATA.
WRI TE MASTER- REC
I NVALI D KEY PERFORM 600- ERR- RTN
END- WRI TE.
400- UPDATE- RTN.
READ I NDEXED- FI LE
I NVALI D KEY PERFORM 600- ERR- RTN
END- READ.
MOVE TRANS- DATA TO MASTER- DATA.
REWRI TE MASTER- REC
I NVALI D KEY PERFORM 600- ERR- RTN
END- REWRI TE.
500- DELETE- RTN.
READ I NDEXED- FI LE
I NVALI D KEY PERFORM 600- ERR- RTN
END- READ.

DELETE I NDEXED- FI LE RECORD
I NVALI D KEY DI SPLAY ’ ERROR ON DELETE’
END- DELETE.
600- ERR- RTN.
DI SPLAY ’ ERROR ’ , TRANS- KEY.
CLOSE TRANS
I NDEXED- FI LE.
STOP RUN.

FOR INDEXED FILE PROCESSING

Us ing ALTERNATE RECORD KEYs
Indexed files may be created with, and accessed by, more than one identifying key field.
That is, we may want to access employee records using either a Social Security number
or a name as the key field. Similarly, we may wish to access an accounts receivable file
by customer number or by customer name, or an inventory file by part number or part
description. To enable a file to be accessed randomly using more than one key field,
we would need to establish an ALTERNATE RECORD KEY.
To establish multiple key fields for indexing, we use an ALTERNATE RECORD KEY clause
in the SELECT statement:


662 ● Unit 5: File Maintenance

SELECT file-name-1
ASSI GN TO implementor-name-1
ORGANI ZATI ON I S I NDEXED
ACCESS MODE I S

H

SEQUENTI AL
RANDOM
DYNAMI C

J

RECORD KEY I S data-name-1

[ALTERNATE RECORD KEY I S data-name-2
[WI TH DUPLI CATES]] . . .


Note the following:
1. More than one ALTERNATE record key can be used.
2. WI TH DUPLI CATES means that an ALTERNATE RECORD KEY need not be unique. Thus,
fields like DEPT- NO or JOB- TI TLE can be used as a key even though numerous
records may have the same DEPT- NO or JOB- TI TLE. Suppose, for example, that
we use DEPT- NO as an ALTERNATE RECORD KEY WI TH DUPLI CATES. We can access
all employees within a given DEPT- NO by using this field as an ALTERNATE RECORD
KEY. If we move 02 to the file’s ALTERNATE RECORD KEY each READ would access
a record in DEPT- NO 02 .
3. A record can be accessed by its RECORD KEY or any of its ALTERNATE RECORD KEYs.

Creating an Indexed File with Alternate Record Keys
Let us first create an indexed file as in Figure 16.9 on page 639, but we will add the
ALTERNATE RECORD KEY clause to the SELECT statement:
Example

SELECT

I NDEXED- PAYROLL- FI LE ASSI GN TO DI SK1

ORGANI ZATI ON I S I NDEXED
ACCESS I S SEQUENTI AL
RECORD KEY I S I NDEXED- SSNO
ALTERNATE RECORD KEY I S I NDEXED- LAST- NAME
WI TH DUPLI CATES.

If the ALTERNATE RECORD KEY is not unique, we use the clause WI TH DUPLI CATES.
The key field(s) specified must be part of the indexed record. COBOL 85 indicates that
keys should be alphanumeric, with a PI C of X’s, but many compilers permit a PI C of
9 ’s as well. If WI TH DUPLI CATES is not specified, the ALTERNATE RECORD KEY must be
unique.
We typically use WI TH DUPLI CATES for a last name that serves as an ALTERNATE
RECORD KEY. Suppose an attempt is made to write a record with an ALTERNATE RECORD
KEY of I NDEXED- LAST- NAME and the name is ’BROWN’ but WI TH DUPLI CATES was not
specified. If a record already exists with the name ’BROWN’, then the following statement
will not write the record but will execute the I NVALI D KEY clause instead:
WRI TE I NDEXED- PAYROLL- REC
I NVALI D KEY PERFORM 700- ERROR- RTN
END- WRI TE.


Note that in Figure 16.9 an I NDEXED- NAME- OUT was defined. For this program, that
field would need to be subdivided:
05

I NDEXED- NAME.
10 I NDEXED- LAST- NAME
10 I NDEXED- FI RST- NAME

PI C X( 12) .
PI C X( 8) .

Chapter 16: Indexed File Processing ● 663
In summary, to create records on disk with last name as an ALTERNATE RECORD KEY
where two or more records might have the same last name, we must use the WI TH
DUPLI CATES clause in the SELECT statement.
As noted, we could also include DEPT- NO, JOB- TI TLE, LEVEL- NO, BI RTH- DATE or any
other field as an ALTERNATE RECORD KEY WI TH DUPLI CATES. For each RECORD KEY or
ALTERNATE RECORD KEY established, an index is created that can be used for accessing
specific records with that KEY.
Accessing Records Randomly by Alternate Record Key

Consider a different program in which we wish to access a record that has I NDEXEDSSNO as its RECORD KEY and I NDEXED- LAST- NAME as its ALTERNATE RECORD KEY. The
program that creates the file has a SELECT clause as defined in the previous example.
The program that accesses the file by key field has the same SELECT clause except that
ACCESS I S RANDOMrather than SEQUENTI AL . In the PROCEDURE DI VI SI ON of the random
access program, we can access the record by either I NDEXED- SSNO, the record key, or
I NDEXED- LAST- NAME, the alternate key.
Interactive Processing. Using interactive processing, we can code the following, which
displays the salary for each Social Security number entered. If, however, the user does
not know the Social Security number but does know the last name, then the last name,
as the alternate record key, can be used for finding the record:
DI SPLAY ’ DO YOU KNOW THE SOCI AL SECURI TY NUMBER ( Y/ N) ?’ .
ACCEPT ANS.
I F ANS = ’ Y’
DI SPLAY ’ ENTER SSNO’
ACCEPT I NDEXED- SSNO
READ I NDEXED- PAYROLL- FI LE
I NVALI D KEY DI SPLAY ’ NO RECORD FOUND’
STOP RUN
END- READ
ELSE

DI SPLAY ’ ENTER LAST NAME’
ACCEPT I NDEXED- LAST- NAME
READ I NDEXED- PAYROLL- FI LE
KEY I S I NDEXED- LAST- NAME kˆˆˆSpecifies access by ALTERNATE
RECORD KEY
I NVALI D KEY
DI SPLAY ’ NO RECORD FOUND’
STOP RUN
END- READ
END- I F.
DI SPLAY ’ SALARY I S ’ , SALARY.

The KEY clause is used with the READ statement when an indexed file has ALTERNATE
RECORD KEYs that we want to use to randomly access a record. If the KEY clause is
omitted when accessing a file randomly, the RECORD KEY is assumed to be the KEY used
for finding the record. Include the KEY clause when you want one of the ALTERNATE
RECORD KEYs to be used for look-up purposes.
Suppose ALTERNATE RECORD KEY WI TH DUPLI CATES was specified in the ENVI RONMENT
DI VI SI ON (as would normally be the case for last name keys) and there is more than
one record with the same ALTERNATE RECORD KEY. The first one that was actually placed

on the disk will be the one retrieved by the READ.

The START State me nt
The START statement enables a program to begin processing an indexed file sequentially
but at a record location other than the first or next physical record in the file. Suppose
an indexed file is in sequence by EMP- NO. We may wish to print, for example, the file
beginning with the employee who has an EMP- NO equal to 008 . Or, we may wish to use

664 ● Unit 5: File Maintenance
the ALTERNATE RECORD KEY to print all employees who have a last name beginning
with the letter ’J’.
The file to be processed this way must include an ORGANI ZATI ON I S I NDEXED clause,
have EMP- NOas the RECORD KEY, and EMP- NAME as an ALTERNATE RECORD KEY. The access
of the file is to be in sequence (ACCESS I S SEQUENTI AL ) if we use the RECORD KEY for
finding a record, even though we want to start the access at some point other than the
beginning. Later on we will see that the ACCESS I S DYNAMI C clause is used if we want
to begin processing an indexed file based on the contents of the ALTERNATE RECORD
KEY.
The format for the START is as follows:
Format


START file-name-1

35
KEY

I
I
I
I
I
I
I
I

S EQUAL TO
S=
S GREATER THAN
S>
S NOT LESS THAN

S NOT <
S GREATER THAN OR EQUAL TO*
S >=*

6 4
data-name-1

[I NVALI D KEY imperative-statement-1]
[NOT I NVALI D KEY imperative-statement-2]*
[END- START]*
*For COBOL 85 only.

Note that the KEY clause is bracketed, meaning that it is optional. Let us begin with
an illustration of how the START statement may be used without a KEY clause.
Consider the following input master file:

01

SELECT MASTER- PAY ASSI GN TO DI SK1
ORGANI ZATI ON I S I NDEXED
ACCESS I S SEQUENTI AL
RECORD KEY I S EMP- NO
ALTERNATE RECORD KEY I S EMP- NAME
WI TH DUPLI CATES.
.
.
.
MASTER- REC.
05 EMP- NO
05 EMP- NAME
.
.
.

Suppose the file is in EMP- NO sequence:
EMP-NO

EMP-NAME

EMP-NO

EMP-NAME

EMP-NO

EMP-NAME

EMP-NO

EMP-NAME

001

BROWN

006

SMITH

008

JONES

015

RICARDO

RECORD 1

RECORD 2

RECORD 3

RECORD 4

To begin processing with an EMP- NO of 008 rather than with the first record, code
the following:
MOVE 008 TO EMP- NO.
The computer points to the
START MASTER- PAY kˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆ record in the file with an
employee number of 008
I NVALI D KEY DI SPLAY ’ EMP- NO 008 DOES NOT EXI ST’
CLOSE MASTER- PAY
STOP RUN
END- START.
READ MASTER- PAY
AT END . . . kˆˆˆ We use AT END here because we will be reading records in
sequence beginning with EMP- NO 008
END- READ.

Chapter 16: Indexed File Processing ● 665
When the record to be accessed has a key equal to the one placed in the RECORD KEY,
the KEY clause in the START statement is not required. In the above, the computer will
locate the record with 008 in EMP- NO, the RECORD KEY. Since 008 has been moved to the
RECORD KEY, the START will locate the record with a RECORD KEY (EMP- NO in this case)
of 008 . The I NVALI D KEY clause is executed only if no such record is found.
Note that the START locates the desired record but it does not READ it into storage.
The record must always be brought into storage with a READ statement. In the preceding
example, the READ follows the START and reads the record with EMP- NO equal to 008 .
When another READ is executed, the next record in EMP- NO sequence would be accessed.
In our illustration, a second read would bring the record with EMP- NO 015 and EMPNAME RI CARDO into storage, since that is the next sequential record in the file.
Suppose we wish to begin processing with an EMP- NO greater than 006 . We must
include a KEY clause with the START because we wish to position the file at a location
greater than the value of a RECORD KEY. The KEY clause can be omitted only if the record
to be located has a RECORD KEY equal to the one stored. We use a KEY clause with the
START as follows:
MOVE 006 TO EMP- NO.
START MASTER- PAY
The computer points to the
KEY > EMP- NO kˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆ first record in the file with an
EMP- NO > 006
I NVALI D KEY DI SPLAY ’ THERE I S NO EMP- NO > 006’
CLOSE MASTER- PAY
STOP RUN
END- START.
READ MASTER- PAY
AT END MOVE ’ NO ’ TO ARE- THERE- MORE- RECORDS
END- READ.

The READ will bring into storage the record with an EMP- NO of 008 , which is the first
RECORD KEY greater than 006 in our illustration. Note that a START locates the record;
a READ is then required to actually access the record. Thus, we must use the KEY clause
with the START statement if the record to be accessed has a primary key greater than
the one specified.

Acce s s ing an Inde xe d File Dynamically
The ACCESS IS DYNAMIC Clause
We have seen how indexed records can be accessed sequentially—when they are originally created and later on for reporting from the file in sequence. For these applications,
we use the ACCESS I S SEQUENTI AL clause in the SELECT statement. Since ACCESS I S
SEQUENTI AL is the default, we can omit this clause entirely when we wish to read from
or write to indexed files in sequence. We have also seen how indexed records can be
accessed randomly—for updating and inquiry purposes. For this, we use the ACCESS
I S RANDOM clause in the SELECT statement.
Sometimes we wish to access an indexed file both randomly and sequentially in a
single program. For this, we say that ACCESS I S DYNAMI C. Suppose we want to do a
random update as in Figures 16.12 or 16.13, but before we end the job we wish to print
a control listing of the entire indexed master file. This is a useful procedure because it
enables the users in the payroll department to check the file. The update would be done
randomly and after it is completed, the printout would require accessing the file sequentially. The ACCESS I S DYNAMI C clause permits both random and sequential access
of an indexed file in one program.
ACCESS IS DYNAMIC and the START Statement
In addition to using ACCESS I S DYNAMI C for combining sequential and random access techniques in a single program, we can use this clause to access records by ALTER-

666 ● Unit 5: File Maintenance
NATE RECORD KEY. Also, when records are to be accessed by both RECORD KEY and
ALTERNATE RECORD KEY, use ACCESS I S DYNAMI C.
When using the START, note the following:

RULES FOR USING THE START STATEMENT
1. The file must be accessed with (a) ACCESS I S SEQUENTI AL for reading records in sequence by the RECORD KEY or (b) ACCESS I S DYNAMI C for reading
records in sequence by an ALTERNATE RECORD KEY.
2. The file must be opened as either input or I - O.
3. If the KEY phrase is omitted, the relational operator ’ I S EQUAL TO’ is implied and the primary record key is assumed to be the key of reference.
4. As we will see, we use KEY =, >, NOT < (or >= with COBOL 85) for accessing
records by ALTERNATE RECORD KEY. We also use KEY >, NOT < (or >= with
COBOL 85) for accessing records by a value that is correspondingly >, NOT <
(or >=) the primary RECORD KEY.

The READ . . . NEXT RECORD . . . Instruction
We have seen that to process records both randomly and sequentially in a single file,
ACCESS I S DYNAMI C must be specified. To indicate that we wish to read records in
sequence by some key field from a file accessed dynamically, we must use a NEXT
RECORD clause. Only the word NEXT is required with this type of READ. Suppose, after
a random update, we wish to access all records in sequence beginning with the record
that has an EMP- NO of 6:
SELECT I NDEXED- PAY ASSI GN TO DI SK1
ORGANI ZATI ON I S I NDEXED
ACCESS I S DYNAMI C
Update procedure is performed
RECORD KEY I S EMP- NO.
..
first using READ . . . I NVALI Dˆl
.
800- PRI NT- CONTROL- LI STI NG.
KEY
MOVE 006 TO EMP- NO.
START I NDEXED- PAY
I NVALI D KEY DI SPLAY ’ EMP- NO 006 DOES NOT EXI ST’
CLOSE I NDEXED- PAY
STOP RUN
END- START.
READ I NDEXED- PAY NEXT RECORD
AT END MOVE ’ NO ’ TO ARE- THERE- MORE- RECORDS
END- READ.
The word NEXT is required
PERFORM 200- SEQ- PRI NT
to perform a sequential
UNTI L NO- MORE- DATA.
.
read of an indexed file
.
.
accessed dynamically
200- SEQ- PRI NT.
.
.
.
READ I NDEXED- PAY NEXT RECORD
AT END MOVE ’ NO ’ TO ARE- THERE- MORE- RECORDS
END- READ.

This NEXT RECORD clause is not necessary when ACCESS I S SEQUENTI AL has been
coded and a standard sequential READ . . . AT END is used throughout the program.
Rather, the READ . . . NEXT RECORD clause is used for (1) sequentially reading from a
file that has been accessed dynamically or for (2) sequentially reading from a file by its
ALTERNATE RECORD KEY, or, as in the preceding case, for (3) beginning a sequential read
from some point other than the beginning of a file.

Chapter 16: Indexed File Processing ● 667
Consider again the update procedure in Figure 16.12 that accesses a file randomly.
Suppose we now want to also access it sequentially for printing a control listing. We
must change the SELECT statement so that ACCESS I S DYNAMI C. Then at 900- END- OFJ OB we code:
900- END- OF- JOB.
MOVE LOW- VALUES TO MASTER- SSNO- I O.
START MASTER- FI LE- I O
kˆˆˆPositions the file at the first record
KEY > MASTER- SSNO- I O
using the MASTER-SSNO-IO RECORD
I NVALI D KEY PERFORM 1100- END- I T
KEY.
END- START.
READ MASTER- FI LE- I O NEXT RECORD kˆˆˆˆˆˆˆˆThe NEXT RECORD clause is
AT END MOVE ’ NO ’ TO ARE- THERE- MORE- RECORDS used when reading sequentially
from a file specified with an
END- READ.
ACCESS IS DYNAMIC clause.
PERFORM 1000- CONTROL- LI ST
Only the word NEXT is required.
UNTI L NO- MORE- RECORDS.
PERFORM 1100- END- I T.
1000- CONTROL- LI ST.
DI SPLAY MASTER- REC- I O.
READ MASTER- FI LE- I O NEXT RECORD
AT END MOVE ’ NO ’ TO ARE- THERE- MORE- RECORDS
END- READ.
1100- END- I T.
CLOSE TRANS- FI LE- I N
MASTER- FI LE- I O.
STOP RUN.

Moving LOW- VALUES to the RECORD KEY called MASTER- SSNO- I O and then starting
the file at a RECORD KEY > MASTER- SSNO- I O ensures that the computer will begin at the
beginning of the file.
If you omit the START in this procedure, the next record to be read will be the one
directly following the last random access. Thus, if the last record updated had a Social
Security number of 882073821, the next sequential Social Security number will be the
one accessed. To begin a sequential access of an indexed file at the beginning of the file
after the file has already been accessed randomly, use the START verb. We also use the START
verb to begin sequential access of a file from some point other than where the file is
currently positioned, as described next.
Sequential Access of Records with the Use of ALTERNATE RECORD KEYs
Suppose you wish to do an alphabetic printing of input records beginning with last
names of ’J ’. This can be accomplished if we have established EMP- LAST- NAME as an
ALTERNATE RECORD KEY:
MOVE ’ J’ TO EMP- LAST- NAME.
START MASTER- PAY
KEY NOT < EMP- LAST- NAME
I NVALI D KEY DI SPLAY ’ ALL EMP- LAST- NAMES BEGI N WI TH A- I ’
CLOSE MASTER- PAY
STOP RUN
END- START.
READ MASTER- PAY NEXT RECORD kˆˆResults in a sequential read
AT END MOVE ’ NO ’ TO ARE- THERE- MORE- RECORDS
END- READ.
PERFORM 200- PRI NT- RTN
UNTI L NO- MORE- RECORDS.
.
.
.
200- PRI NT- RTN.
MOVE EMP- LAST- NAME TO LAST- NAME- OUT.
WRI TE PRI NT- REC FROM NAME- REC.
READ MASTER- PAY NEXT RECORD kˆˆResults in a sequential read
AT END MOVE ’ NO ’ TO ARE- THERE- MORE- RECORDS
END- READ.

668 ● Unit 5: File Maintenance
In this case EMP- LAST- NAME is the ALTERNATE RECORD KEY, and a sequential read
means reading records in sequence by that ALTERNATE KEY. Thus, the NEXT RECORD
clause in the READ MASTER- PAY instruction is necessary when we wish to access the
records sequentially by an ALTERNATE RECORD KEY.
The records in our file would print as follows:
JONES
RI CARDO
SMI TH

Note that these records print in order by last name even though the file is not in
alphabetic order. This is because the ALTERNATE RECORD KEY index itself is stored in
sequence. Hence, each READ can result in a sequential access by last name because the
computer looks up the next sequential last name from the EMP- LAST- NAME index.
Other Uses of the START
Suppose our MASTER- PAY file was created with DEPT- NO and YEAR- OF- BI RTH as two
other ALTERNATE RECORD KEYs. The following is the create program’s SELECT statement:
SELECT MASTER- PAY ASSI GN TO ’ PAY. I DX’
ORGANI ZATI ON I S I NDEXED
ACCESS I S SEQUENTI AL
RECORD KEY I S SSNO
ALTERNATE RECORD KEY I S
EMP- LAST- NAME WI TH DUPLI CATES
ALTERNATE RECORD KEY I S
DEPT WI TH DUPLI CATES
ALTERNATE RECORD KEY I S
YR- OF- BI RTH WI TH DUPLI CATES.

To access this file using an ALTERNATE RECORD KEY, ACCESS I S DYNAMI C should be
coded in place of ACCESS I S SEQUENTI AL in the above.
Example 1

Display the names of all employees in DEPT 02 .
MOVE 02 TO DEPT.
START MASTER- PAY
KEY = DEPT
I NVALI D KEY DI SPLAY ’ NO DEPT 02 RECORDS’
CLOSE MASTER- PAY
STOP RUN
END- START.
READ MASTER- PAY NEXT RECORD
AT END MOVE ’ NO ’ TO MORE- RECS
END- READ.
PERFORM 200- DI SPLAY- I T
UNTI L DEPT > 02
OR MORE- RECS = ’ NO ’ .
CLOSE MASTER- PAY.
STOP RUN.
200- DI SPLAY- I T.
DI SPLAY EMP- LAST- NAME
EMP- FI RST- NAME.
READ MASTER- PAY NEXT RECORD
AT END MOVE ’ NO ’ TO MORE- RECS
END- READ.

Example 2

Assuming a current year of 1994, display the total number of employees who are under 18 years
old (YR- OF- BI RTH > 76 ):
MOVE 76 TO YR- OF- BI RTH.
START MASTER- PAY
KEY > YR- OF- BI RTH

Chapter 16: Indexed File Processing ● 669
I NVALI D KEY DI SPLAY ’ NO ONE I S UNDER 18’
CLOSE MASTER- PAY
STOP RUN
END- START.
READ MASTER- PAY NEXT RECORD
AT END MOVE ’ NO ’ TO MORE- RECS
END- READ.
PERFORM 200- CALC
UNTI L MORE- RECS = ’ NO ’ .
DI SPLAY ’ TOTAL NO. OF EMPLOYEES UNDER 18 = ’ , CTR.
CLOSE MASTER- PAY.
STOP RUN.
200- CALC.
ADD 1 TO CTR.
READ MASTER- PAY NEXT RECORD
AT END MOVE ’ NO ’ TO MORE- RECS
END- READ.

This program excerpt would be of more use if it calculated the YR- OF- BI RTH of employees under
18 years old by subtracting 18 from the current year.

SELF-TEST

Consider the following format for records to be written to an indexed file:
FD
01

1.

STUDENT- FI LE
LABEL RECORDS ARE STANDARD.
STUDENT- REC.
05 SSNO
PI C 9( 9) .
05 NAME
PI C X( 20) .
05 SCHOOL
PI C 9.
88 LI BERAL- ARTS
88 BUSI NESS
88 ENGI NEERI NG
05 MAJOR
PI C X( 4) .
05 GPA
PI C 9V99.
05 NO- OF- CREDI TS
PI C 9( 3) .

VALUE 1.
VALUE 2.
VALUE 3.

Write the SELECT statement to create the file so that SSNO is the RECORD KEY and the other
fields are ALTERNATE RECORD KEYs.

Use ALTERNATE RECORD KEYs to access records for the following:
2.
3.
4.
5.

Solutions

1.

Write a program excerpt to display the names of all students with GPAs greater than 3.0.
Write a program excerpt to display the names of all students in the School of Business.
Write a program excerpt to display the total number of students in the School of Business
with GPAs , 3.0. Hint: You need to do a sequential search of School of Business students.
Write a program excerpt to display the names of all students who have more than 120 credits.
SELECT STUDENT- FI LE
ASSI GN TO DI SK1
ORGANI ZATI ON I S I NDEXED
ACCESS I S SEQUENTI AL
RECORD KEY I S SSNO
ALTERNATE RECORD KEY I S
NAME WI TH DUPLI CATES
ALTERNATE RECORD KEY I S
SCHOOL WI TH DUPLI CATES
ALTERNATE RECORD KEY I S
MAJOR WI TH DUPLI CATES
ALTERNATE RECORD KEY I S
GPA WI TH DUPLI CATES
ALTERNATE RECORD KEY I S
NO- OF- CREDI TS WI TH DUPLI CATES.

670 ● Unit 5: File Maintenance
For Questions 2–5, the file must have an ACCESS I S DYNAMI C clause in the SELECT statement if
all the routines are part of the same program. All other clauses in the SELECT statement will be
the same.
2.
MOVE 3. 0 TO GPA.

3.

4.

START STUDENT- FI LE
KEY > GPA
I NVALI D KEY
DI SPLAY ’ THERE ARE NO RECORDS WI TH GPA > 3. 0’
CLOSE STUDENT- FI LE
STOP RUN
END- START.
READ STUDENT- FI LE NEXT RECORD
AT END MOVE ’ NO ’ TO MORE- RECS
END- READ.
PERFORM 200- CALC
UNTI L MORE- RECS = ’ NO ’ .
CLOSE STUDENT- FI LE.
STOP RUN.
200- CALC.
DI SPLAY NAME.
READ STUDENT- FI LE NEXT RECORD
AT END MOVE ’ NO ’ TO MORE- RECS
END- READ.
MOVE 1 TO SCHOOL.
START STUDENT- FI LE
KEY > SCHOOL
I NVALI D KEY DI SPLAY ’ SCHOOL ERROR’
CLOSE STUDENT- FI LE
STOP RUN
END- START.
READ STUDENT- FI LE NEXT RECORD
AT END MOVE ’ NO ’ TO MORE- RECS
END- READ.
PERFORM 200- CALC
UNTI L SCHOOL > 2
OR MORE RECS = ’ NO ’ .
CLOSE STUDENT- FI LE.
STOP RUN.
200- CALC.
DI SPLAY NAME.
READ STUDENT- FI LE NEXT RECORD
AT END MOVE ’ NO ’ TO MORE- RECS
END- READ.
MOVE 1 TO SCHOOL.
START STUDENT- FI LE
KEY > SCHOOL
I NVALI D KEY DI SPLAY ’ SCHOOL ERROR’
CLOSE STUDENT- FI LE
STOP RUN
END- START.
READ STUDENT- FI LE NEXT RECORD
AT END MOVE ’ NO ’ TO MORE- RECS
END- READ.
PERFORM 200- CALC
UNTI L SCHOOL > 2 OR
MORE RECS = ’ NO ’ .
DI SPLAY ’ NUMBER OF BUSI NESS STUDENTS WI TH GPA > 3. 0 ’ , CTR.
CLOSE STUDENT- FI LE.
STOP RUN.

Chapter 16: Indexed File Processing ● 671
200- CALC.
I F GPA > 3. 0
ADD 1 TO CTR
END- I F.
READ STUDENT- FI LE NEXT RECORD
AT END MOVE ’ NO ’ TO MORE- RECS
END- READ.

5.

MOVE 120 TO NO- OF- CREDI TS.
START STUDENT- FI LE
KEY > NO- OF- CREDI TS
I NVALI D KEY DI SPLAY ’ NO RECORDS > 120’
CLOSE STUDENT- FI LE
STOP RUN
END- START.
READ STUDENT- FI LE NEXT RECORD
AT END MOVE ’ NO ’ TO MORE- RECS
END- READ.
PERFORM 200- CALC
UNTI L MORE- RECS = ’ NO ’ .
CLOSE STUDENT- FI LE.
STOP RUN.
200- CALC.
DI SPLAY NAME.
READ STUDENT- FI LE NEXT RECORD
AT END MOVE ’ NO ’ TO MORE- RECS
END- READ.

In summary, a START is commonly used when we wish to:
1. Start processing an indexed file with the RECORD KEY equal to some value. For
this, the clause ’ KEY 4 value’ is optional. The START statement will position the
file pointer at the record whose RECORD KEY is equal to the value in the RECORD
KEY field.
2. Start processing an indexed file with the RECORD KEY greater than or .4 some
value. For this, the clause ’KEY > value’, ’KEY NOT < value’, or ’KEY >= value’
(COBOL 85) is required. The START statement will position the file pointer at the
record whose RECORD KEY is >, not = (COBOL 85) the value in the RECORD
KEY field.
3. Use the ALTERNATE RECORD KEY to control how an indexed file is to be accessed.
For this, the clause ’KEY = alternate-record-key-name’ may be used. The START
statement will position the file pointer at the record whose corresponding ALTERNATE RECORD KEY is equal to the value in that ALTERNATE RECORD KEY. We can
alternatively use the clause KEY >, NOT = (COBOL 85) alternate-recordkey-name.
We typically precede a START statement by moving a value to the RECORD KEY for
numbers 1 and 2 above. For number 3, we precede the START by moving a value to the
ALTERNATE RECORD KEY. We typically follow a START statement with a READ.
A Review of INVALID KEY Clauses
A review of how the I NVALI D KEY clause is used with input-output verbs follows:

672 ● Unit 5: File Maintenance
WHAT INVALID KEY MEANS FOR AN INDEXED FILE OPENED AS I-O
Operation
READ
WRI TE

J

REWRI TE
DELETE
START

Meaning
There is no existing record on the file with the specified key.
The file already contains a record with the specified key. (It can
also mean that the disk space allocated for the file has been exceeded.)
No record with the specified key can be found on the file.

ALTERNATE RECORD KEYs Reduce the Need for Multiple Copies of a File
Suppose you want to print out four reports from a file in four different sequences—in
EMPLOYEE- NAME, BI RTH- DATE, DEPT- NO, and JOB- TI TLE sequence. If these fields are not
established as ALTERNATE RECORD KEYs (either WI TH or without DUPLI CATES as applicable), then the file would need to be sorted into the desired sequence for each printout
and each sorted version processed sequentially. This results in multiple copies of a file,
each in a different sequence. Maintaining multiple files can be difficult, time-consuming,
and error-prone. That is, if one version of the file is updated, steps must be taken to
ensure that all other versions of the file are updated at the same time, for data validation
purposes.
Establishing an indexed file with one primary RECORD KEY and numerous ALTERNATE
RECORD KEYs (WI TH or without DUPLI CATES) reduces the need for multiple files and
enables a single file to be updated as needed. Database management systems make
extensive use of indexes with ALTERNATE RECORD KEYs to reduce duplication of effort.
Note, too, that two consecutive fields can be concatenated or linked to make one
ALTERNATE RECORD KEY. EMP- LAST- NAME and EMP- FI RST- NAME, for example, can be
described as part of a group item called EMP- NAME that can be defined as an ALTERNATE
RECORD KEY.

The FILE STATUS Claus e
Consider the following coding for an indexed file to be created as output:
WRI TE I NDEXED- PAY- REC
I NVALI D KEY DI SPLAY ’ A WRI TE ERROR HAS OCCURRED’
END- WRI TE.

If the I NVALI D KEY clause is executed, we know that a write error occurred, but we
do not really know what specifically caused the error. It could be a duplicate key error,
a sequence error, or some other error.
The FILE STATUS clause can be used with the SELECT statement to determine the
exact type of input or output error that has occurred when either reading from or
writing to a file.
The SELECT statement for an indexed file could include FI LE STATUS as its last clause:
Format

SELECT . . .

[FI LE STATUS is data-name]
where the data-name specified must appear in WORKI NG- STORAGE as a two-position alphanumeric field.

Chapter 16: Indexed File Processing ● 673
Example

SELECT I NDEXED- PAY- FI LE . . .
FI LE STATUS I S WS- STATUS.
.
.
.
WORKI NG- STORAGE SECTI ON.
01 WS- STATUS
PI C X( 2) .

When an input or output operation is performed on I NDEXED- PAY- FI LE, the operating system
will place a value in WS- STATUS, which may be tested by the programmer in the PROCEDURE
DI VI SI ON. The result of an IyO operation can thus be more specifically determined.

The following are possible values that may be placed in the FI LE STATUS field when
an input or output operation is performed. FI LE STATUS can be used with any type of
file; we highlight those values specifically relating to indexed files with an p. Note that
if the leftmost character in the FI LE STATUS field is a 0, the I/O operation was successfully completed. If it is not zero, then the I/O operation resulted in an error.
Contents of the FI LE
STATUS field after an
input or output operation

Meaning

Successful Completion
p 00
p 02

04

Successful completion—no error occurred.
The record being processed has a duplicate alternate record
key. (Note: This is not an error when your program includes
WI TH DUPLI CATES for the ALTERNATE RECORD KEY.)
A READ statement has been successfully completed, but the
length of the record does not conform to the File Description
specifications.

Unsuccessful Completion
p 10
p 21
p 22
p 23
p 24

p 30
34
37

41
42
p 43

9x

A sequential READ statement (READ . . . AT END) has been
attempted, but there are no more input records.
A sequence error has occurred—keys are not in the correct
order.
An attempt was made to write a record that would create a
duplicate primary record key.
The required record was not found during a READ.
A boundary error has occurred—an attempt has been made to
write beyond the preestablished boundaries of an indexed file
as established by the operating system.
A permanent data error has occurred (this is a hardware
problem.)
A boundary error for a sequential file has occurred.
A permanent error has occurred because an OPEN statement
has been attempted on a file that will not support the mode
specified in the OPEN statement (e.g., an indexed file is opened
as OUTPUT when ACCESS I S RANDOM has been specified, or a
print file is opened as I - O).
An OPEN statement has been attempted on a file that is already
open.
A CLOSE statement has been attempted on a file that has not
been opened.
An attempt has been made to DELETE or REWRI TE a record
after an unsuccessful READ (e.g., there is no record in storage
to delete or rewrite).
Codes of 91–99 are specifically defined by the implementor—
consult your user’s manual.

The preceding are just some of the values that the FI LE STATUS field can contain. As
noted, the FI LE STATUS clause can be used with any type of file, not only indexed files.
The p entries, however, are those that apply specifically to indexed files.

674 ● Unit 5: File Maintenance
Using the FI LE STATUS field, we can display a more meaningful message if an input
or output error occurs. Consider the following output routine:
WRI TE I NDEXED- PAY- REC
I NVALI D KEY PERFORM 500- ERROR- RTN
END- WRI TE.
I F WS- STATUS = ’ 00’
PERFORM 600- OK- RTN
ENDI F.
.
.
.
500- ERROR- RTN.
I F WS- STATUS = ’ 21’
DI SPLAY ’ KEY I S NOT I N SEQUENCE ’ ,
EMP- NO- RECORD- KEY
END- I F.
I F WS- STATUS = ’ 22’
DI SPLAY ’ DUPLI CATE KEY ’ ,
EMP- NO- RECORD- KEY
ENDI F.
.
.
.

Each time an input or output operation is performed on a file with a FI LE STATUS
clause, the specified data-name will be reset with a new value.

Exce ptio n Handling w ith the USE State me nt
We have seen how an I NVALI D KEY clause can be used to indicate when an input or
output error has occurred. The FI LE STATUS field defined in WORKI NG- STORAGE can be
used to clarify the type of error that has occurred.
The most comprehensive method for handling input/output errors is to establish a
separate section or sections in a program. This technique is known as error or exception
handling because it processes all ‘exceptions to the rules.’ The exception handling routines are placed in the DECLARATI VES segment of the PROCEDURE DI VI SI ON, which itself
consists of one or more sections. This segment always appears first in the PROCEDURE
DI VI SI ON. It must begin with a section-name. The USE statement is coded in the section
within the DECLARATI VES portion of the program:

DECLARATI VES.

section-name SECTI ON.
USE AFTER STANDARD

H

J

EXCEPTI ON
ERROR

PROCEDURE

ON file-name-1 ...
END DECLARATI VES.

The words ERROR and EXCEPTI ON mean the same thing and can be used interchangeably. END DECLARATI VES does not have a hyphen.
The following program excerpt will illustrate how exception handling is performed
with the USE statement:
SELECT I NDEXED- FI LE . . .
FI LE- STATUS I S TEST- I
.
.
.
01 TEST- I O.
05 CHAR1
PI C
05 CHAR2
PI C
.
.
.
PROCEDURE DI VI SI ON.
DECLARATI VES.
A000- EXCEPTI ON- HANDLI NG SECTI

O.

X.
X.

Section header must
follow DECLARATIVES
ON.

Chapter 16: Indexed File Processing ● 675

Tests the value of the
FILE STATUS field

INVALID KEY is not
specified because error
handling is performed
in the DECLARATIVES
segment

USE AFTER ERROR PROCEDURE kˆˆˆˆˆˆˆ Will invoke the following
paragraphs if any I/O
ON I NDEXED- FI LE.
error occurs with this file
A100- CHECK- I T.
I F TEST- I O = ’ 23’
DI SPLAY ’ REQUI RED RECORD NOT FOUND’
ENDI F.
.
.
.
END DECLARATI VES.
B000- REGULAR PROCESSI NG SECTI ON. kˆˆˆˆˆˆ Once a section header is
.
.
used, as above, the rest of
.
the PROCEDURE DIVISION
READ
I
NDEXEDFI
LE.
.
must be divided into
.
.
sections
WRI TE I NDEXED- FI LE.

5
H

The DECLARATI VES SECTI ON pertaining to the USE AFTER statement will automatically
be executed by the computer whenever an input or output error has occurred. Thus
there is no need for I NVALI D KEY clauses to display error messages.

SELF-TEST

1.
2.

To read an indexed file sequentially beginning at some point other than the first record in
the file, you must use the
statement.
(T or F) The following is a valid use of the START verb if PART- NO is the RECORD KEY. Indicate
what the instructions accomplish:
MOVE 123 TO PART- NO.
START I NVENTORY- FI LE
I NVALI D KEY PERFORM 700- ERR- RTN
END- START.

3.

(T or F) To position a file at PART- NO 123 if PART- NO is an ALTERNATE RECORD KEY code:
MOVE 123 TO PART- NO.
START I NVENTORY- FI LE
KEY = PART- NO
I NVALI D KEY PERFORM 700- ERR- RTN
END- START.

4.
5.
6.
7.

8.

(T or F) The START instruction actually reads a record into storage.
To read an indexed file both randomly and sequentially in the same program, you must
specify ACCESS I S
in the SELECT statement.
(T or F) More than one ALTERNATE RECORD KEY may be established for a single indexed file.
Suppose I TEM- DESCRI PTI ON is an ALTERNATE RECORD KEY for an indexed file called I NVENTORY. Code the program excerpt to read the record with an I TEM- DESCRI PTI ON of
’WI DGETS’.
Suppose the following clause is coded with the SELECT statement:
FI LE STATUS I S WS- STATUS.

9.
10.

Solutions

1.
2.
3.
4.
5.

After a READ . . . I NVALI D KEY . . . , WS- STATUS will indicate
.
(T or F) If you code a USE AFTER ERROR PROCEDURE ON file-name statement, you need not
use an I NVALI D KEY with the READ or WRI TE statement to display error messages.
(T or F) The USE statement is coded in the DECLARATI VES segment of a program.
START

T—The file is positioned at the record with a PART- NO of 123 ; this record will be brought
into storage when the next READ is executed.
T—The KEY = clause is required because we wish to access a record by an ALTERNATE RECORD
KEY rather than its RECORD KEY.
F—It positions the index at the correct location, but a READ statement must be executed to
input a record.
DYNAMI C