COBOL file status

File status 22: Duplicate key

File status 22 means a WRITE or REWRITE tried to store a record with a key that already exists. On an indexed file it's a duplicate prime key or a duplicate unique alternate key. On a relative file the relative record number is already in use.

Other names for this code

Also written as FS 22, file status code 22, COBOL status 22, VSAM status 22, duplicate key COBOL, status key 22, SK22.

What it means

Status 22 is an invalid key condition, and the record was not written. It applies to indexed files (VSAM KSDS) and relative files (VSAM RRDS). QSAM and line-sequential files have no keys and can't return it.

There are three ways to hit it. A WRITE uses a prime key that's already in the file. A WRITE or REWRITE gives an alternate key a value that already exists, and that alternate key wasn't declared WITH DUPLICATES. Or, on a relative file, a WRITE targets a relative record number whose slot already holds a record.

VSAM reports the condition as register 15 = 8 with feedback code 8 (X'08'), an attempt to store a record with a duplicate key. If data-name-8 is coded on the FILE STATUS clause, it holds those values. The INVALID KEY phrase runs if it's coded. If the duplicate is in an alternate key declared WITH DUPLICATES, there's no error at all, and the program sees 02 instead.

Common causes

  • A rerun after a failure. The first run added some records before it stopped, and the rerun tries to add them again.
  • The input holds the same key twice, for example a transaction file with repeated entries.
  • The program means to update an existing record but uses WRITE instead of REWRITE.
  • An alternate index was defined UNIQUEKEY, but the data now has two records with the same alternate value.
  • The key field wasn't filled correctly, so many records get the same key, often spaces or zeros.

How to find the problem

  1. DISPLAY the key of the failing record. Then READ the file with that key, or look it up with a file browse tool, to see the record that's already there.
  2. If the prime key isn't a duplicate, check the alternate keys. IDCAMS LISTCAT ALL on each alternate index shows whether it's UNIQUEKEY and whether it's in the upgrade set.
  3. Display data-name-8. Return code 8 with feedback code 8 confirms a duplicate key at the VSAM level.
  4. For a rerun, check the restart logic and how far the failed run got before it stopped.

How to fix it

  • For add-or-update logic, READ first and choose WRITE or REWRITE from the result. Or try the WRITE and, on 22, READ the record and REWRITE it.
  • Make the job safe to rerun. Restore the file from a backup taken before the failed run, or skip keys that already exist.
  • Remove duplicate input before the update with a sort step, for example DFSORT with SUM FIELDS=NONE.
  • If duplicate alternate keys are valid, redefine the alternate index NONUNIQUEKEY and add WITH DUPLICATES to the ALTERNATE RECORD KEY clause.

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

What is the difference between file status 21 and 22?

22 means the key already exists. 21 means a key arrived out of ascending order in sequential access, or the prime key changed before a REWRITE.

Why do I get 22 on a REWRITE?

A REWRITE can't create a duplicate prime key, but it can give a unique alternate key a value another record already has. Check the alternate keys.

Is file status 22 like SQLCODE -803 in Db2?

Yes, it's the same idea for VSAM files. Both mean an insert or update would break a unique key.