VSAM

VSAM Alternate Index Duplicate Keys: Diagnose File Status 22 Before Changing the Load

A COBOL load can write successfully to a VSAM KSDS until one record fails with file status `22`. When the cluster has an alternate index, the visible primary key may be unique while a different field is violating a uniqueness rule. This incident guide shows how to prove which key failed and how to choose a safe correction without turning a data-control problem into a blind rerun.

KeyFile operationStatus checkRecovery action
Original DBala learning flow — a visual summary of this article’s practical decision path.

A COBOL load can write successfully to a VSAM KSDS until one record fails with file status 22. When the cluster has an alternate index, the visible primary key may be unique while a different field is violating a uniqueness rule. This incident guide shows how to prove which key failed and how to choose a safe correction without turning a data-control problem into a blind rerun.

The customer and account names below are fictional. VSAM definitions, file-status conventions, security controls, and permitted repair procedures vary by site.

Symptom: a new primary key, but `WRITE` returns 22

Consider a nightly customer-reference load. The base KSDS uses an eight-byte customer number as its primary key. An alternate index makes a 12-byte external reference searchable by another application.

A support analyst sees this sequence in the job output:

READ input record: customer number 00481729
WRITE CUSTOMER-MASTER returned file status 22
step ended with application RC=12

It is tempting to say, “the customer already exists.” That conclusion is unsafe. File status 22 indicates a duplicate key condition for an operation that requires uniqueness. If the base key is new, the duplicate can instead be the alternate key. It can also reveal that the application is writing a changed external reference that already belongs to a different base record.

The first question is therefore not *how do we restart?* It is *which uniqueness rule rejected this record, and what does that rule mean to the business?*

Evidence to capture before anyone changes data

Capture evidence while the failing run, selected input generation, and catalog definitions are still identifiable. Do not put a full customer record into broadly visible spool or a ticket merely to investigate one key.

| Evidence | Why it matters |
| --- | --- |
| Job ID, step name, program version, and business date | Separates this run from earlier executions or a different load cycle |
| Resolved input dataset and record sequence/correlation key | Lets the team locate the exact input safely |
| COBOL file status immediately after WRITE or REWRITE | Confirms the failure belongs to this operation, not a later one |
| Base cluster name, alternate-index name, and path name actually allocated | Identifies the object and access path in use |
| Primary key and alternate-key value, handled under local masking rules | Enables controlled comparison without copying unrelated fields |
| Whether the record was intended as insert, update, or replay | Changes the meaning of a duplicate |
| Existing base record(s) found by the alternate key | Distinguishes a true business collision from a replay or stale reference |

A listing of the program is useful, but it is not enough. The DD statement or dynamic allocation may resolve a different cluster from the one a developer expects, and an old catalog definition can have different uniqueness behavior from the current design document.

Diagnosis path: start with the catalog, not the correction

An alternate index (AIX) is a separate VSAM index built from a field in records of a related base cluster. A path is commonly used by applications to access that relationship. The AIX definition decides whether each alternate-key value is unique or whether several base records may share it.

A simplified, fictional definition looks like this:

DEFINE AIX (NAME(APP.CUST.EXTREF.AIX) -
            RELATE(APP.CUSTOMER.KSDS) -
            KEYS(12 41) -
            UNIQUEKEY -
            UPGRADE)
DEFINE PATH (NAME(APP.CUST.EXTREF.PATH) -
             PATHENTRY(APP.CUSTOMER.KSDS APP.CUST.EXTREF.AIX))

Here, KEYS(12 41) means that the alternate key is 12 bytes beginning at offset 41 in the base record. UNIQUEKEY says no two base records may have the same 12-byte external reference. UPGRADE is intended to keep the alternate index synchronized when qualifying base records are changed through supported VSAM processing. The exact IDCAMS syntax and local naming standards should be checked against the definitions in the target environment.

Use a controlled catalog inquiry or the organization’s approved VSAM administration process to answer these questions:

1. Is the failing object a base cluster, an AIX, or a path to the AIX?
2. Is the AIX defined as UNIQUEKEY or NONUNIQUEKEY?
3. Does the alternate-key position and length match the copybook used by the running program?
4. Is the AIX related to the same base cluster that the job allocated?
5. Is the AIX expected to be maintained with UPGRADE, and is that maintenance path operational?

Then inspect the data through an authorized utility or application inquiry. Locate the existing base record associated with the alternate-key value. Compare only the business-relevant identifiers: for example, source-system ID, effective date, record state, and load-cycle marker. Do not assume that matching alternate keys are accidental; some domains legitimately reuse a reference and should use a nonunique design or a compound key instead.

A compact COBOL pattern that preserves the clue

The file status must be checked immediately. A later READ, START, or error paragraph can replace the status and destroy the most useful evidence.

       WRITE CUSTOMER-RECORD
           INVALID KEY
               MOVE CUSTOMER-STATUS TO WS-WRITE-STATUS
               PERFORM BUILD-DUPLICATE-EVIDENCE
               MOVE 12 TO RETURN-CODE
           NOT INVALID KEY
               ADD 1 TO WS-WRITTEN-COUNT
       END-WRITE

IF WS-WRITE-STATUS = '22'
PERFORM LOG-MASKED-KEY-CONTEXT
END-IF
```

The shape matters more than the paragraph names. Preserve the operation, file status, controlled record locator, and key values before any additional file I/O. INVALID KEY handling and file-status behavior should follow the compiler, access mode, and local coding standard in use; test the exact pattern in a non-production environment.

For a REWRITE, the analysis has an extra branch. The base record may already exist correctly, but the new alternate-key bytes could collide with another record. Compare the old and proposed alternate-key values. A load that treats every input row as an insert may also be replaying rows that a previous committed run already applied.

Decision points: correction depends on the reason

| Finding | Safe direction |
| --- | --- |
| Same input row was already committed in an earlier run | Prove the replay with the run control and target record; use the approved restart/idempotency procedure rather than deleting data |
| External reference is legitimately shared | Confirm the business rule and design. A nonunique AIX or a different access design may be required, but this is a controlled change, not an overnight utility fix |
| External reference should be unique but was assigned twice upstream | Reject or hold the incoming item under the interface process; preserve the collision evidence for the data owner |
| Copybook offsets or AIX definition disagree | Stop the load. Correcting one side without impact analysis can misindex many records |
| AIX is stale or maintenance is suspected to have failed | Escalate to the VSAM/storage owner with catalog and integrity evidence; do not rebuild or delete an index without an approved recovery plan |

Changing UNIQUEKEY to NONUNIQUEKEY simply to finish tonight’s job is particularly risky. It changes a data invariant for every consumer of that path and can make a formerly unambiguous lookup return multiple records. Likewise, deleting the existing base record may conceal a replay while destroying a record still required by another process.

Before rerun

Practical takeaway

File status 22 is a signal to identify the uniqueness rule that was enforced, not a command to remove a “duplicate.” When a KSDS has an alternate index, prove the base cluster, path, AIX definition, alternate-key value, and prior-processing state before deciding whether the right answer is a controlled reject, a restart, a data correction, or a design change.

← Back to all articles