Microsoft KB Archive/63006

COBOL Example of ALTERNATE RECORD KEY as Part of RECORD KEY

PSS ID Number: Q63006 Article last modified on 04-20-1993

3.00 3.00a | 3.00 3.00a MS-DOS | OS/2

Summary: In Microsoft COBOL Compiler versions 3.0 and 3.0a for MS-DOS and MS OS/2, an ALTERNATE RECORD KEY can be defined for ISAM files. This ALTERNATE KEY can be used to index the file instead of the primary key. If the alternate key is declared as a field that is already used as part of the RECORD KEY in the FILE SECTION, the alternate key must REDEFINE the primary key. You can use either the RECORD KEY or the ALTERNATE RECORD KEY to access the file.

More Information: The following is a code example:

INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT CUST-FILE ASSIGN TO DISK &quot;CUSTOMER.DAT&quot; ORGANIZATION IS INDEXED ACCESS MODE IS DYNAMIC STATUS IS WS-FILE-STAT RECORD KEY IS CUST-KEY ALTERNATE RECORD KEY IS ALT-CUST-LAST-NAME WITH DUPLICATES. DATA DIVISION. FILE SECTION. FD CUST-FILE LABEL RECORDS ARE STANDARD DATA RECORD IS CUST-RECORD. 01 CUST-RECORD. 03 CUST-KEY. 05 CUST-NUMBER        PIC X(04). 05 CUST-LAST-NAME     PIC X(30). 03 CUST-ALT-KEY REDEFINES CUST-KEY. 05 FILLER             PIC X(04). 05 ALT-CUST-LAST-NAME PIC X(30). 03 CUST-FIRST-NAME       PIC X(20). 03 CUST-ORDER-AMT        PIC 9(05)V99. WORKING-STORAGE SECTION. 01 WS-FILE-STAT                 PIC XX. 88 SUCCESSFUL-COMPLETION    VALUE '00'. 88 END-OF-FILE              VALUE '10'. 88 NO-RECORD-FOUND          VALUE '23'. 01 WS-SWITCHES. 05 CUST-FILE-SWITCH         PIC X VALUE 'N'. 88 CUST-FILE-EOF         VALUE 'Y'. PROCEDURE DIVISION. OPEN I-O CUST-FILE. IF SUCCESSFUL-COMPLETION NEXT SENTENCE ELSE DISPLAY &quot;Customer file open failed&quot; DISPLAY WS-FILE-STAT STOP RUN. MOVE LOW-VALUES TO ALT-CUST-LAST-NAME. START CUST-FILE KEY >= ALT-CUST-LAST-NAME INVALID KEY MOVE &quot;Y&quot; TO CUST-FILE-SWITCH. PERFORM READ-FILE UNTIL CUST-FILE-SWITCH = &quot;Y&quot;. CLOSE CUST-FILE. STOP RUN. READ-FILE. READ CUST-FILE NEXT RECORD AT END MOVE &quot;Y&quot; TO CUST-FILE-SWITCH. IF CUST-FILE-SWITCH = &quot;Y&quot; DISPLAY &quot;AT END OF FILE&quot; ELSE DISPLAY CUST-RECORD. Additional reference words: 3.00 3.00a Copyright Microsoft Corporation 1993.