CPF5009: Duplicate record key in member
CPF5009 means a write or update would have given a record the same key as another record in a member that requires unique keys. It is a diagnostic message that names the two records, and it is normally followed by CPF5026.
Other names for this code
Also written as CPF5009 duplicate record key, MSGID CPF5009, duplicate record key in member, CPF5009 CPF5026, duplicate key IBM i.
What it means
CPF5009 has the text "Duplicate record key in member" followed by the member name. It is a diagnostic message with severity 10. It doesn't stop anything by itself. Its job is to explain the failure that comes next, which is usually the notify message CPF5026, duplicate key not allowed.
The detail is in the second-level text. Press F1 on the message and it gives the member number, record number and record format of the record the program tried to write or update. It also gives the record that already has the same key. When the record number of the failing record is zero, the duplicate came from an output operation, a new record that was never stored. A non-zero number points to an update that would have changed the key of an existing record.
The member named isn't always the file the program opened. A physical file can have several logical files and SQL indexes built over it. If any of them requires unique keys, a write to the physical file fails when it would break that rule. CPF5009 then names the member of the unique logical file or index, which can be a file the program never refers to.
Common causes
- The program wrote a new record with a key that is already in the file, for example an order number that was handed out twice.
- An update changed a key field to a value another record already has.
- A logical file with UNIQUE in its DDS, or a unique SQL index or constraint, is built over the physical file and uses a different key from the one the program reads by.
- A load or conversion program ran twice, or ran against a file that wasn't cleared first.
- A trigger program on the file writes to another file, and that write breaks a unique key there.
How to find the problem
- Run DSPJOBLOG, put the cursor on CPF5009 and press F1. Note the member and library named, and the two record numbers.
- If the member named isn't the file the program opens, run
DSPDBR FILE(ACCTLIB/on the physical file to list the logical files over it, thenORDHDR) DSPFD FILE(ACCTLIB/to see its keys and whether they must be unique.ORDHDRL3) TYPE(*ACCPTH) - Look at the existing record with SQL using its record number, for example
SELECT * FROM ACCTLIB.ORDHDR WHERE RRN(ORDHDR) = 1376. - Compare its key with the key the program was writing. The program's variables in a dump or in debug show the new value.
How to fix it
- Use a key that isn't taken. If keys come from a counter, check that the counter wasn't reset or restored to an old value.
- If the record already exists and should be changed, update it instead of writing a new one.
- If the unique rule is wrong for the business, change the logical file, index or constraint. Do this as a design change, because other programs may rely on the rule.
- For a rerun of a load, clear the target first or make the load skip records that already exist.
- Have the program handle the error. RPG returns status 01021 and COBOL returns status 22, so the program can report the record and carry on.
What it looks like
Duplicate record key in member ORDHDRL3. Duplicate key not allowed for member ORDHDRL3.
IBM documentation
IBM Display Message Descriptions (DSPMSGD). 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.
Read the installed message description:
DSPMSGD MSGID(CPF5009) MSGF(QSYS/QCPFMSG)
Also inspect the full job log and second-level message text, including substitution values.
Related codes
Questions
What does record number zero mean in CPF5009?
It means the failing operation was a write of a new record, which had no record number yet. A non-zero number means an update that tried to change an existing record's key.
Why does CPF5009 name a file my program doesn't use?
A unique logical file or index is built over the physical file you wrote to. The database checks every unique access path over the file, and CPF5009 names the one that was broken.
Do I need to monitor for CPF5009?
Usually not. It is diagnostic. Handle CPF5026, or the language status it becomes, and read CPF5009 when you need the record details.