Microsoft KB Archive/50883

COBOL 3.0 ISAM file Corrupted When Key Length > 69 Characters

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

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

Summary: The program shown below demonstrates a case where an ISAM file can become corrupted, even though the program code is correct. This is an intermittent problem with ISAM files and appears only in special cases under certain conditions. A single case of this problem is demonstrated below. Microsoft has confirmed this to be a problem in Microsoft COBOL versions 3.0 and 3.0a for MS OS/2 and MS-DOS. This problem has been corrected in Microsoft COBOL Professional Development System version 4.0 for MS-DOS and MS OS/2.

More Information: The program below, BISAM.COB, creates 100 records in blocks of 10, deletes every other block of 10, writes 100 more records, and then deletes every 5th block of 10 records. While deleting every 5th block of 10 records, the program stops with an invalid key error, even though the records should be there, the keys are valid, and the code is completely correct. The file status returns a 23, indicating an invalid key with no record found, which is incorrect. In the program example below, the problem seems to be related to the size of the RECORD KEY. TR-DUMMY and WS-DUMMY are the largest part of the RECORD key for the two files. If the TR-DUMMY and WS-DUMMY are decreased in size from PIC X(64) to PIC X(63) or anything smaller (making a key length of 69 or smaller), the program runs correctly. Also, if the order of the keys TR-RECORD-TYPE and TR-RECORD-N are reversed, the program runs correctly. To compile and link the program, use the following command lines: COBOL BISAM.COB ; LINK BISAM ;

Code Example
$SET ANS85 IDENTIFICATION DIVISION. PROGRAM-ID. INDTST. ENVIRONMENT DIVISION. CONFIGURATION SECTION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT TR-FILE ASSIGN DISK ORGANIZATION INDEXED ACCESS DYNAMIC RECORD KEY TR-RECORD-KEY. DATA DIVISION. FILE SECTION. FD TR-FILE VALUE OF FILE-ID IS 'EDISR.FIL' LABEL RECORDS OMITTED. 01 TR-RECORD. 02 TR-RECORD-KEY. 03 TR-RECORD-TYPE                    PIC 9(2). 03 TR-RECORD-N                       PIC 9(4). 03 TR-DUMMY                          PIC X(64). 02 FILLER                              PIC X(1). WORKING-STORAGE SECTION. 01 WS-RECORD. 02 WS-RECORD-KEY. 03 KEY-TYPE               PIC 9(2). 03 KEY-N                  PIC 9(4). 03 WS-DUMMY               PIC X(64). 02 FILLER                   PIC X(1). PROCEDURE DIVISION. 010-MAIN. OPEN OUTPUT TR-FILE. CLOSE TR-FILE. OPEN I-O TR-FILE. DISPLAY 'Check - before 1'. MOVE SPACES TO WS-DUMMY. PERFORM 100-WRITE-10-TRRECS THRU 100-EXIT VARYING KEY-N FROM 1 BY 1 UNTIL KEY-N > 10. DISPLAY 'Check - before 2' PERFORM 200-DELETE-10-TRRECS THRU 200-EXIT VARYING KEY-N FROM 1 BY 2 UNTIL KEY-N > 10. DISPLAY 'Check - before 3' PERFORM 100-WRITE-10-TRRECS THRU 100-EXIT VARYING KEY-N FROM 11 BY 1 UNTIL KEY-N > 20. DISPLAY 'Check - before 4' PERFORM 200-DELETE-10-TRRECS THRU 200-EXIT VARYING KEY-N FROM 11 BY 5 UNTIL KEY-N > 20. CLOSE TR-FILE. STOP RUN. 100-WRITE-10-TRRECS. PERFORM VARYING KEY-TYPE FROM 0 BY 1 UNTIL KEY-TYPE > 9 MOVE WS-RECORD TO TR-RECORD WRITE TR-RECORD INVALID KEY DISPLAY 'Invalid key on write' CLOSE TR-FILE STOP RUN END-WRITE END-PERFORM. 100-EXIT. 200-DELETE-10-TRRECS. PERFORM VARYING KEY-TYPE FROM 0 BY 1 UNTIL KEY-TYPE > 9 MOVE WS-RECORD TO TR-RECORD DELETE TR-FILE INVALID KEY DISPLAY 'Invalid key on delete' CLOSE TR-FILE STOP RUN END-DELETE END-PERFORM. 200-EXIT. Additional reference words: 3.00 3.00a Copyright Microsoft Corporation 1993.