• Tidak ada hasil yang ditemukan

Directory UMM :wiley:Public:college:cobol:

N/A
N/A
Protected

Academic year: 2017

Membagikan "Directory UMM :wiley:Public:college:cobol:"

Copied!
15
0
0

Teks penuh

(1)

STOP RUN. 200- CALC- RTN.

MOVE TRANS- KEY TO MASTER- KEY. COBOL 74 users

should substitute nestedI Fs

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.

ADDITIONAL

OPTIONS FOR

INDEXED

F

ILE

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 ora 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 anALTERNATE RECORD KEY.

(2)

SELECTfile-name-1

ASSI GN TOimplementor-name-1

ORGANI ZATI ON I S I NDEXED

SEQUENTI AL RANDOM

ACCESS MODE I S

H

J

DYNAMI C

RECORD KEY I Sdata-name-1

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

Note the following:

1. More than oneALTERNATErecord key can be used.

2. WI TH DUPLI CATESmeans that anALTERNATE RECORD KEYneed not be unique. Thus, fields likeDEPT- NOor J OB- TI TLEcan be used as a key even though numerous records may have the sameDEPT- NOor J OB- TI TLE. Suppose, for example, that we useDEPT- NOas anALTERNATE RECORD KEY WI TH DUPLI CATES. We can access allemployees within a givenDEPT- NOby using this field as anALTERNATE RECORD KEY. If we move02to the file’sALTERNATE RECORD KEYeachREADwould access a record inDEPT- NO 02.

3. A record can be accessed by itsRECORD KEYor any of itsALTERNATE 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 KEYclause to theSELECTstatement:

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 theALTERNATE RECORD KEYis not unique, we use the clauseWI TH DUPLI CATES. The key field(s) specified must be part of the indexed record. COBOL 85 indicates that keys should be alphanumeric, with aPI CofX’s, but many compilers permit aPI Cof

9’s as well. IfWI TH DUPLI CATESis not specified, theALTERNATE RECORD KEYmust be unique.

We typically use WI TH DUPLI CATESfor a last name that serves as an ALTERNATE RECORD KEY. Suppose an attempt is made to write a record with anALTERNATE RECORD KEYof I NDEXED- LAST- NAMEand 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 willnotwrite the record but will execute theI NVALI D KEYclause instead:

WRI TE I NDEXED- PAYROLL- REC

I NVALI D KEY PERFORM 700- ERROR- RTN END- WRI TE.

Note that in Figure 16.9 anI NDEXED- NAME- OUTwas defined. For this program, that field would need to be subdivided:

05 I NDEXED- NAME.

(3)

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 CATESclause in theSELECTstatement.

As noted, we could also includeDEPT- NO,J OB- TI TLE,LEVEL- NO,BI RTH- DATEor any other field as anALTERNATE RECORD KEY WI TH DUPLI CATES. For eachRECORD KEYor

ALTERNATE RECORD KEYestablished, an index is created that can be used for accessing specific records with thatKEY.

Accessing Records Randomly by Alternate Record Key

Consider a different program in which we wish to access a record that hasI NDEXED-SSNOas itsRECORD KEYand I NDEXED- LAST- NAMEas itsALTERNATE RECORD KEY. The program that creates the file has aSELECTclause as defined in the previous example. The program that accesses the file by key field has thesameSELECTclause except that

ACCESS I S RANDOMrather thanSEQUENTI AL. In thePROCEDURE DI VI SI ONof the random access program, we can access the record by eitherI 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.

TheKEYclause is used with theREADstatement when an indexed file hasALTERNATE RECORD KEYs that we want to use to randomly access a record. If the KEYclause is omitted when accessing a file randomly, theRECORD KEYis assumed to be theKEYused for finding the record. Include theKEYclause when you want one of theALTERNATE RECORD KEYs to be used for look-up purposes.

SupposeALTERNATE RECORD KEY WI TH DUPLI CATESwas specified in theENVI RONMENT DI VI SI ON(as would normally be the case for last name keys) and there is more than one record with the sameALTERNATE RECORD KEY. The first one that was actually placed on the disk will be the one retrieved by theREAD.

The START State me nt

(4)

EMP-NO

001

EMP-NAME EMP-NO EMP-NAME EMP-NO EMP-NAME EMP-NO EMP-NAME

BROWN 006 SMITH 008 JONES 015 RICARDO RECORD 1 RECORD 2 RECORD 3 RECORD 4

theALTERNATE RECORD KEYto print all employees who have a last name beginning with the letter ’J’.

The file to be processed this way must include anORGANI ZATI ON I S I NDEXEDclause, haveEMP- NOas theRECORD KEY, andEMP- NAMEas anALTERNATE RECORD KEY. The access of the file is to be in sequence (ACCESS I S SEQUENTI AL) if we use theRECORD KEYfor finding a record, even though we want to start the access at some point other than the beginning. Later on we will see that theACCESS I S DYNAMI Cclause is used if we want to begin processing an indexed file based on the contents of theALTERNATE RECORD KEY.

The format for theSTARTis as follows:

Format I S EQUAL TO

I S =

I S GREATER THAN I S >

STARTfile-name-1 KEY I S NOT LESS THAN data-name-1

I S NOT <

3 5

I S GREATER THAN OR EQUAL TO*

6 4

I S >=*

[I NVALI D KEY imperative-statement-1] [NOT I NVALI D KEY imperative-statement-2]* [END- START]*

*For COBOL 85 only.

Note that theKEYclause is bracketed, meaning that it is optional. Let us begin with an illustration of how theSTARTstatement may be used without aKEYclause.

Consider the following input master file:

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.

. . .

01 MASTER- REC. 05 EMP- NO 05 EMP- NAME .

. .

Suppose the file is inEMP- NOsequence:

To begin processing with anEMP- NOof008rather than with the first record, code the following:

MOVE 008 TO EMP- NO.

START MASTER- PAY kˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆ

The computer points to the record in the file with an employee number of008 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 useAT ENDhere because we will be reading records in sequence beginning withEMP- NO 008

(5)

When the record to be accessed has a key equal to the one placed in theRECORD KEY, theKEYclause in theSTARTstatement is not required. In the above, the computer will locate the record with008inEMP- NO, theRECORD KEY. Since008has been moved to the

RECORD KEY, theSTARTwill locate the record with aRECORD KEY(EMP- NOin this case) of008. TheI NVALI D KEYclause is executed only if no such record is found.

Note that theSTART locatesthe desired record but it does notREADit into storage. The record must always be brought into storage with aREADstatement. In the preceding example, theREADfollows theSTART and reads the record withEMP- NOequal to008. When anotherREADis executed, thenextrecord inEMP- NOsequence would be accessed. In our illustration, a second read would bring the record withEMP- NO 015and EMP-NAME RI CARDOinto storage, since that is the next sequential record in the file.

Suppose we wish to begin processing with an EMP- NOgreater than006. We must include aKEYclause with theSTARTbecause we wish to position the file at a location greater thanthe value of aRECORD KEY. TheKEYclause can be omitted only if the record to be located has aRECORD KEYequal tothe one stored. We use aKEYclause with the

STARTas follows:

MOVE 006 TO EMP- NO. START MASTER- PAY

KEY > EMP- NOkˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆ

The computer points to the 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.

TheREADwill bring into storage the record with anEMP- NOof008, which is the first

RECORD KEYgreater than006in our illustration. Note that aSTARTlocates the record; aREADis then required to actually access the record. Thus, we must use theKEYclause with theSTART 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 orig-inally created and later on for reporting from the file in sequence. For these applications, we use theACCESS I S SEQUENTI ALclause in theSELECTstatement. SinceACCESS I S SEQUENTI ALis 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 theACCESS I S RANDOMclause in theSELECTstatement.

Sometimes we wish to access an indexed fileboth randomly and sequentially in a single program. For this, we say thatACCESS 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 se-quentially. TheACCESS I S DYNAMI Cclause permits both random and sequential access of an indexed file in one program.

ACCESS IS DYNAMIC and the START Statement

(6)

ALTER-NATE RECORD KEY. Also, when records are to be accessed by both RECORD KEYand

ALTERNATE RECORD KEY, useACCESS I S DYNAMI C. When using theSTART, note the following:

The READ . . . NEXT RECORD . . . Instruction

We have seen that to process records both randomly and sequentially in a single file,

ACCESS I S DYNAMI Cmust 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 RECORDclause. Only the wordNEXTis required with this type ofREAD. Suppose, after a random update, we wish to access all records in sequence beginning with the record that has anEMP- NOof 6:

Update procedure is performed first usingREAD . . . I NVALI Dˆl KEY

SELECT I NDEXED- PAY ASSI GN TO DI SK1 ORGANI ZATI ON I S I NDEXED ACCESS I S DYNAMI C RECORD KEY I S EMP- NO. .

. .

800- PRI NT- CONTROL- LI STI NG. 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.

PERFORM 200- SEQ- PRI NT UNTI L NO- MORE- DATA. .

. .

200- SEQ- PRI NT. .

. .

The wordNEXTis required to perform a sequential read of an indexed file accessed dynamically

READ I NDEXED- PAY NEXT RECORD

AT END MOVE ’ NO ’ TO ARE- THERE- MORE- RECORDS END- READ.

ThisNEXT RECORDclause is not necessary when ACCESS I S SEQUENTI AL has been coded and a standard sequential READ . . . AT ENDis used throughout the program. Rather, theREAD . . . NEXT RECORDclause 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.

RULES FOR USING THE START STATEMENT

1. The file must be accessed with (a)ACCESS I S SEQUENTI ALfor reading rec-ords in sequence by theRECORD KEYor (b)ACCESS I S DYNAMI Cfor reading records in sequence by anALTERNATE RECORD KEY.

2. The file must be opened as either input orI - O.

3. If theKEYphrase is omitted, the relational operator’ I S EQUAL TO’ is im-plied and the primary record key is assumed to be the key of reference. 4. As we will see, we useKEY =,>,NOT <(or>=with COBOL 85) for accessing

records byALTERNATE RECORD KEY. We also useKEY >,NOT <(or>=with COBOL 85) for accessing records by a value that is correspondingly>,NOT <

(7)

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 theSELECTstatement so thatACCESS I S DYNAMI C. Then at900- END- OF-J OBwe code:

900- END- OF- J OB.

MOVE LOW- VALUES TO MASTER- SSNO- I O. START MASTER- FI LE- I O

KEY > MASTER- SSNO- I O

I NVALI D KEY PERFORM 1100- END- I T END- START.

kˆˆˆPositions the file at the first record using the MASTER-SSNO-IO RECORD KEY.

READ MASTER- FI LE- I O NEXT RECORD

AT END MOVE ’ NO ’ TO ARE- THERE- MORE- RECORDS END- READ.

PERFORM 1000- CONTROL- LI ST UNTI L NO- MORE- RECORDS. PERFORM 1100- END- I T. 1000- CONTROL- LI ST.

DI SPLAY MASTER- REC- I O.

kˆˆˆˆˆˆˆˆThe NEXT RECORD clause is used when reading sequentially from a file specified with an ACCESS IS DYNAMIC clause. Only the word NEXT is required.

READ MASTER- FI LE- I O NEXT RECORD

AT END MOVE ’ NO ’ TO ARE- THERE- MORE- RECORDS

Moving LOW- VALUESto the RECORD KEYcalled MASTER- SSNO- I Oand then starting the file at aRECORD KEY > MASTER- SSNO- I Oensures that the computer will beginat the beginning of the file.

If you omit theSTARTin 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 theSTARTverb. We also use theSTART

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 establishedEMP- LAST- NAMEas 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

(8)

In this caseEMP- LAST- NAMEis the ALTERNATE RECORD KEY, and a sequential read means reading records in sequence by thatALTERNATE KEY. Thus, the NEXT RECORD

clause in the READ MASTER- PAYinstruction is necessary when we wish to access the records sequentially by anALTERNATE RECORD KEY.

The records in our file would print as follows:

J ONES RI CARDO SMI TH

Note that these records print in order by last name even though the file isnot in alphabetic order. This is because theALTERNATE RECORD KEYindex itself is stored in sequence. Hence, eachREADcan result in a sequential access by last name because the computer looks up the next sequential last name from theEMP- LAST- NAMEindex.

Other Uses of the START

Suppose our MASTER- PAYfile was created with DEPT- NOand YEAR- OF- BI RTHas two otherALTERNATE RECORD KEYs. The following is the create program’sSELECTstatement:

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 anALTERNATE RECORD KEY,ACCESS I S DYNAMI Cshould be coded in place ofACCESS I S SEQUENTI ALin the above.

Example 1 Display the names of all employees inDEPT 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

(9)

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 theYR- OF- BI RTHof 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 STUDENT- FI LE

LABEL RECORDS ARE STANDARD. 01 STUDENT- REC.

05 SSNO PI C 9( 9) . 05 NAME PI C X( 20) . 05 SCHOOL PI C 9.

88 LI BERAL- ARTS VALUE 1.

88 BUSI NESS VALUE 2.

88 ENGI NEERI NG VALUE 3. 05 MAJ OR PI C X( 4) .

05 GPA PI C 9V99.

05 NO- OF- CREDI TS PI C 9( 3) .

1. Write theSELECTstatement to create the file so thatSSNOis theRECORD KEYand the other fields areALTERNATE RECORD KEYs.

UseALTERNATE RECORD KEYs to access records for the following:

2. Write a program excerpt to display the names of all students with GPAs greater than 3.0. 3. Write a program excerpt to display the names of all students in the School of Business. 4. 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. 5. Write a program excerpt to display the names of all students who have more than 120 credits.

Solutions 1. 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

MAJ OR WI TH DUPLI CATES ALTERNATE RECORD KEY I S

GPA WI TH DUPLI CATES ALTERNATE RECORD KEY I S

(10)

For Questions 2–5, the file must have anACCESS I S DYNAMI Cclause in theSELECTstatement if all the routines are part of the same program. All other clauses in theSELECTstatement will be the same.

2. MOVE 3. 0 TO GPA. 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.

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

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

(11)

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, aSTARTis commonly used when we wish to:

1. Start processing an indexed file with theRECORD KEYequal to some value. For this, the clause’ KEY4value’ is optional. TheSTARTstatement will position the file pointer at the record whoseRECORD KEYis equal to the value in theRECORD KEYfield.

2. Start processing an indexed file with theRECORD KEYgreater than or.4some value. For this, the clause ’KEY > value’, ’KEY NOT <value’, or ’KEY >= value’ (COBOL 85) is required. TheSTARTstatement will position the file pointer at the record whoseRECORD KEYis>, not<, or>=(COBOL 85) the value in theRECORD KEYfield.

3. Use theALTERNATE RECORD KEYto control how an indexed file is to be accessed. For this, the clause ’KEY = alternate-record-key-name’ may be used. TheSTART

statement will position the file pointer at the record whose corresponding ALTER-NATE RECORD KEYis equal to the value in thatALTERNATE RECORD KEY. We can alternatively use the clause KEY >, NOT <, or>= (COBOL 85) alternate-record-key-name.

We typically precede aSTART statement by moving a value to theRECORD KEYfor numbers 1 and 2 above. For number 3, we precede theSTARTby moving a value to the

ALTERNATE RECORD KEY. We typically follow aSTARTstatement with aREAD.

A Review of INVALID KEY Clauses

(12)

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 theI NVALI D KEYclause 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.

TheFILE STATUS clause can be used with theSELECTstatement to determine the exact type of input or output error that has occurred when either reading from or writing to a file.

TheSELECTstatement for an indexed file could includeFI LE STATUSas its last clause:

Format SELECT . . .

[FI LE STATUSis data-name]

where the data-name specified must appear inWORKI NG- STORAGEas a two-position al-phanumeric field.

WHAT INVALID KEY MEANS FOR AN INDEXED FILE OPENED AS I-O Operation Meaning

READ There is no existing record on the file with the specified key.

WRI TE The file already contains a record with the specified key. (It can also mean that the disk space allocated for the file has been ex-ceeded.)

REWRI TE DELETE

J

START

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, andJ OB- TI TLEsequence. If these fields are not established asALTERNATE RECORD KEYs (eitherWI THor withoutDUPLI CATESas appli-cable), 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 primaryRECORD KEYand numerousALTERNATE RECORD KEYs (WI THor 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 withALTERNATE RECORD KEYs to reduce duplication of effort. Note, too, that two consecutive fields can be concatenated or linked to make one

(13)

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 onI NDEXED- PAY- FI LE, the operating system will place a value in WS- STATUS, which may be tested by the programmer in thePROCEDURE 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 theFI LE STATUSfield when an input or output operation is performed.FI LE STATUScan be used withanytype of file; we highlight those values specifically relating to indexed files with anp. Note that

if the leftmost character in the FI LE STATUSfield is a 0, the I/O operation was suc-cessfully completed. If it is not zero, then the I/O operation resulted in an error.

Contents of theFI LE STATUSfield after an

input or output operation Meaning

Successful Completion

p00 Successful completion—no error occurred.

p02 The record being processed has a duplicate alternate record key. (Note:This isnotan error when your program includes WI TH DUPLI CATESfor theALTERNATE RECORD KEY.)

04 AREADstatement has been successfully completed, but the length of the record does not conform to the File Description specifications.

Unsuccessful Completion

p10 A sequentialREADstatement (READ . . . AT END) has been attempted, but there are no more input records.

p21 A sequence error has occurred—keys are not in the correct order.

p22 An attempt was made to write a record that would create a duplicate primary record key.

p23 The required record was not found during aREAD.

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

p30 A permanent data error has occurred (this is a hardware problem.)

34 A boundary error for a sequential file has occurred. 37 A permanent error has occurred because anOPENstatement

has been attempted on a file that will not support the mode specified in theOPENstatement (e.g., an indexed file is opened asOUTPUTwhenACCESS I S RANDOMhas been specified, or a print file is opened asI - O).

41 AnOPENstatement has been attempted on a file that is already open.

42 ACLOSEstatement has been attempted on a file that has not been opened.

p43 An attempt has been made toDELETEorREWRI TEa record after an unsuccessfulREAD(e.g., there is no record in storage to delete or rewrite).

9x Codes of 91–99 are specifically defined by the implementor— consult your user’s manual.

(14)

Using theFI LE STATUSfield, 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 END- I 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 END- I F.

. . .

Each time an input or output operation is performed on a file with aFI 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 anI NVALI D KEYclause can be used to indicate when an input or output error has occurred. TheFI LE STATUSfield defined inWORKI NG- STORAGEcan 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 sectionsin a program. This technique is known as error orexception handlingbecause it processes all ‘exceptions to the rules.’ The exception handling rou-tines are placed in theDECLARATI VESsegment of thePROCEDURE DI VI SI ON, which itself consists of one or more sections. This segment always appears first in thePROCEDURE DI VI SI ON. It must begin with a section-name. TheUSEstatement is coded in the section within theDECLARATI VESportion of the program:

DECLARATI VES.

section-nameSECTI ON.

EXCEPTI ON

USE AFTER STANDARD

H

J

PROCEDURE ERROR

ONfile-name-1 ...

END DECLARATI VES.

The wordsERRORandEXCEPTI ONmean the same thing and can be used interchange-ably.END DECLARATI VESdoes not have a hyphen.

The following program excerpt will illustrate how exception handling is performed with theUSEstatement:

SELECT I NDEXED- FI LE . . . FI LE- STATUS I S TEST- I O. .

. .

01 TEST- I O.

05 CHAR1 PI C X.

05 CHAR2 PI C X.

. . .

PROCEDURE DI VI SI ON. DECLARATI VES.

A000- EXCEPTI ON- HANDLI NG SECTI ON.

(15)

USE AFTER ERROR PROCEDURE ON I NDEXED- FI LE.

kˆˆˆˆˆˆˆ Will invoke the following paragraphs if any I/O error occurs with this file Tests the value of the

FILE STATUS field

5

A100- CHECK- I T.

I F TEST- I O = ’ 23’

DI SPLAY ’ REQUI RED RECORD NOT FOUND’ END- I F.

B000- REGULAR PROCESSI NG SECTI ON.. .

.

READ I NDEXED- FI LE. .

. .

WRI TE I NDEXED- FI LE.

kˆˆˆˆˆˆOnce a section header is used, as above, the rest of the PROCEDURE DIVISION must be divided into sections

TheDECLARATI VES SECTI ONpertaining to theUSE AFTERstatement will automatically be executed by the computer whenever an input or output error has occurred. Thus there is no need forI NVALI D KEYclauses to display error messages.

SELF-TEST 1. To read an indexed file sequentially beginning at some point other than the first record in the file, you must use the statement.

2. (T or F) The following is a valid use of theSTARTverb ifPART- NOis theRECORD 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 atPART- NO 123ifPART- NOis anALTERNATE RECORD KEYcode: MOVE 123 TO PART- NO.

START I NVENTORY- FI LE KEY = PART- NO

I NVALI D KEY PERFORM 700- ERR- RTN END- START.

4. (T or F) TheSTARTinstruction actually reads a record into storage.

5. To read an indexed file both randomly and sequentially in the same program, you must specifyACCESS I S in theSELECTstatement.

6. (T or F) More than oneALTERNATE RECORD KEYmay be established for a single indexed file. 7. SupposeI TEM- DESCRI PTI ONis anALTERNATE RECORD KEYfor an indexed file calledI

NVEN-TORY. Code the program excerpt to read the record with an I TEM- DESCRI PTI ON of ’WI DGETS’.

8. Suppose the following clause is coded with theSELECTstatement: FI LE STATUS I S WS- STATUS.

After aREAD . . . I NVALI D KEY . . .,WS- STATUSwill indicate .

9. (T or F) If you code aUSE AFTER ERROR PROCEDURE ONfile-name statement, you need not use anI NVALI D KEYwith theREADorWRI TEstatement to display error messages.

10. (T or F) TheUSEstatement is coded in theDECLARATI VESsegment of a program.

Solutions 1. START

2. T—The file is positioned at the record with aPART- NOof123; this record will be brought into storage when the nextREADis executed.

3. T—TheKEY =clause is required because we wish to access a record by anALTERNATE RECORD KEYrather than itsRECORD KEY.

4. F—It positions the index at the correct location, but aREADstatement must be executed to input a record.

Referensi

Dokumen terkait

Penelitian ini bertujuan untuk mengidentifikasi keterlibatan stakeholder dalam penetapan status kawasan, pemetaan interaksi antara pemangku kepentingan, dan

Waktu yang tepat untuk menggosok gigi adalah.... Sehabis makan dan sebelum

Penghayatan amalan moral dan akhlak yang baik dalam kalangan pelajar diharap dapat merealisasikan matlamat pendidikan negara serta mencapai salah satu hasrat

ANALISIS KINERJA PEMASARAN PEMBIAYAAN MURABAHAH PADA NASABAH UKM BANK SUMUT SYARIAH STABAT.. R Aldri Kurnia 1 , Rismayani 2 , Sugih Arto

Disain penelitian adalah uji eksperimental laboratorium dengan 2 (dua) macam perlakuan (minyak hasil penggorengan singkong dan daging) dan 4 (empat) kali pengulangan dengan

Supervisor adalah orang yang melakukan kegiatan supervisi. Supervisor dapat dilakukan oleh pengawas sekolah, kepala sekolah, karena ia memiliki tanggung jawab tentang mutu

Manfaat yang diharapkan dari penelitian ini adalah aplikasi ini dapat menjadi alat bantu yang akan digunakan perusahaan pada perekrutan karyawan baru sehingga

Pada saat perolehan, aset murabahah tidak diakui sebagai persediaan sebesar harga perolehan. Bank Muamalat Cabang Pekanbaru melakukan pembelian terhadap aset