CPF4174: OPNID for file already exists
CPF4174 means an OPNQRYF or OPNDBF command tried to open a file with an open identifier (OPNID) that another open in the job is still using. The earlier open was never closed, so the new one is refused.
Other names for this code
Also written as CPF4174 OPNID already exists, MSGID CPF4174, OPNID for file already exists, OPNQRYF CPF4174, OPNDBF CPF4174.
What it means
CPF4174 is an escape message from the open commands used in CL programs. A typical example reads "OPNID(ORDHDR) for file ORDHDR already exists." The Open Query File (OPNQRYF) and Open Database File (OPNDBF) commands give each open a name, called the open identifier. Close File (CLOF) uses that name later to close the right open.
IBM's rule for OPNID is that the identifier must differ from that of any other file opened with OPNDBF or OPNQRYF that hasn't been closed yet. If you don't give OPNID, the command uses the file name, so two opens of the same file without names clash straight away.
The open is refused and nothing new is opened. The earlier open is still there, with its own selection, sort and file position. A program that carries on regardless may read the old query's records, which can be more confusing than the error itself.
Common causes
- A CL program ran OPNQRYF, then failed or was cancelled before it reached CLOF. When it runs again in the same job, the old open is still there.
- An interactive program runs OPNQRYF, calls an RPG program, and the user then starts a second inquiry from that RPG program that runs the same OPNQRYF again in the same job.
- A CL loop opens a file with OPNDBF or OPNQRYF on each pass and only closes it after the loop ends.
- Two different CL programs in one job open the same file without giving distinct OPNID values.
- Opens with OPNSCOPE(*JOB) stay in place until they are closed or the job ends, so they can clash with programs in other activation groups.
How to find the problem
- Run DSPJOBLOG and press F1 on CPF4174. Look for the earlier OPNQRYF or OPNDBF in the log and check whether a CLOF followed it.
- Run
DSPJOB OPTION(*OPNF)in the job, or use the open files option on WRKJOB, to see the file still open and how many times. - Look at the CL program's error paths. A MONMSG that jumps to the end of the program, or a cancel from an inquiry message, often skips the CLOF.
How to fix it
- Close the old open first. Put
CLOF OPNID(ORDHDR)followed byMONMSG MSGID(CPF4519 CPF4520)just before the open. CPF4520 is sent when nothing is open with that identifier, so the MONMSG makes the close safe to run every time. - Make sure every exit path of the CL program, including error paths, runs CLOF.
- Give each open its own name with the OPNID parameter when two programs in the same job need the same file at the same time.
- Run each inquiry in its own named activation group, so its opens and overrides are scoped to it and don't collide with another inquiry in the same job.
- Avoid OPNSCOPE(*JOB) unless you need it. It makes the open live until the job ends or CLOF runs, which widens the chance of a clash.
- For new code, consider embedded SQL instead of OPNQRYF. SQL cursors are opened and closed inside the program and don't use OPNID.
What it looks like
OPNID(ORDHDR) for file ORDHDR already exists.
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(CPF4174) MSGF(QSYS/QCPFMSG)
Also inspect the full job log and second-level message text, including substitution values.
Related codes
Questions
Why does the program work the first time and fail the second?
The first run left its OPNQRYF open, usually because it ended before CLOF. The second run in the same job finds the identifier still in use. Signing off and on clears it, but a CLOF with MONMSG at the start of the program is the real fix.
What is the default OPNID?
If you don't give one, the open is named after the file. That is why two unnamed opens of the same file in one job clash.
Can I check for an open before I close it?
There is no simple test command. The usual pattern is to run CLOF and monitor for CPF4520, which only means nothing was open under that name.