COBOL file status

File status 02: Success, duplicate alternate key

File status 02 means a READ, WRITE or REWRITE on an indexed file worked, and an alternate key value involved is shared with another record. It is a successful status, and it only appears for alternate keys declared WITH DUPLICATES.

Other names for this code

Also written as FS 02, file status code 02, COBOL status 02, VSAM status 02, status key 02, SK02.

What it means

The first digit of a COBOL file status gives the class of result. A first digit of 0 means the statement worked. Status 02 adds one fact. The file is indexed, it has an alternate record key declared WITH DUPLICATES, and the record just read or written shares its alternate key value with another record.

On a READ by the alternate key, IBM sets 02 when the next record in that key order has the same value as the one just returned. If three records share a value and you read through them in sequence, you normally see 02, 02 and then 00 on the last one. On a WRITE or REWRITE, 02 means the record was stored and its alternate key now matches a value that was already there.

At the VSAM level the request ended with register 15 = 0 and feedback code 8 (X'08'). VSAM uses that code to say a duplicate key follows (on a GET) or that a duplicate key was created in a nonunique alternate index (on a PUT). Because the return code is zero, the second status area (data-name-8 of the FILE STATUS clause) isn't filled in. QSAM and line-sequential files have no keys, so they never return 02.

Common causes

  • The program tests IF WS-FILE-STATUS = '00' and treats anything else as a failure, so a normal duplicate alternate key stops the job. This is the usual reason 02 gets noticed at all.
  • The file is read through an alternate key that many records share, such as a postcode, a department or a customer name.
  • A record was added or updated with an alternate key value that other records already have, on an alternate index defined NONUNIQUEKEY.
  • A program was changed to read through an alternate index path instead of the prime key, and its status checks were never widened.

How to find the problem

  1. DISPLAY the file status right after the statement that seemed to fail. If it shows 02, the I/O worked and the problem is in the program's status test.
  2. Check the SELECT clause for ALTERNATE RECORD KEY IS ... WITH DUPLICATES. Only a key declared that way can produce 02.
  3. Run IDCAMS LISTCAT ENTRIES(...) ALL against the alternate index. The attributes section shows whether its keys are unique or nonunique.
  4. If you log the status after each READ, a run of 02 values that ends in 00 is the normal pattern for one group of duplicates.

How to fix it

  • Treat any status that starts with 0 as success. Test the first character of the status, or use a condition name such as 88 FS-OK VALUE '00' '02'.
  • Use 02 on purpose when you process groups. Keep reading while the status is 02, and treat the 00 that follows as the last record of the group.
  • If the business rule says the alternate key must be unique, the 02 is a warning about the data. Define the alternate index UNIQUEKEY and drop WITH DUPLICATES. A duplicate then fails with status 22 instead of being stored.

IBM documentation

IBM Enterprise COBOL documentation. Use the documentation for your installed release.

This link opens the relevant IBM reference library or lookup instructions. Match the complete code and your installed product release; it is not a code-specific review.

Report a correction to WND Software.

Questions

Is file status 02 an error?

No. The statement worked. It only tells you the alternate key value is shared with another record.

Why do I get 02 on most records of a group and 00 on the last one?

On a READ, 02 says another record with the same alternate key comes next. The last record of a group has nothing after it with that value, so it returns 00.

Can a QSAM or line-sequential file return 02?

No. Status 02 needs an indexed file with an alternate key that allows duplicates, which on z/OS means a VSAM KSDS with an alternate index.