Db2 SQLCODE

SQLCODE -204: Undefined object name

SQLCODE -204 (SQLSTATE 42704) means the SQL names a table, view, alias or other object that doesn't exist in this Db2 subsystem under the name Db2 worked out. A wrong schema qualifier is the usual reason.

Also written as SQL -204, SQLCODE=-204, SQLCODE = -204, db2 -204, sqlstate 42704, SQL0204N, is an undefined name.

What it means

Every table, view, alias and sequence in Db2 has a two-part name, a schema and an object name, such as PAYPROD.EMP_HIST. Programs often leave the schema off and write just EMP_HIST. Db2 then fills in the schema. For static SQL in a package it uses the QUALIFIER bind option, or the package owner if no QUALIFIER was given. For dynamic SQL it normally uses the CURRENT SCHEMA special register, which starts out equal to CURRENT SQLID.

If no object exists with the resulting full name, Db2 returns -204 and puts the name it looked for in SQLERRMC. Compare that name with the one you expected.

Static SQL can get -204 at bind time or at run time. With VALIDATE(BIND), the bind fails and no new package is made. With VALIDATE(RUN), the bind ends with warnings and Db2 checks the name again each time the statement runs.

Common causes

  • The package was bound with a QUALIFIER or owner from another environment. EMP_HIST resolves to PAYTEST.EMP_HIST in production, or the other way round.
  • The table, view or alias was never created in this subsystem, or it was dropped for a change and not yet recreated.
  • The object name is misspelled in the program.
  • A dynamic SQL user, for example in SPUFI or QMF, has a different CURRENT SQLID or CURRENT SCHEMA from the one the query was written for.
  • An alias still exists but the table behind it was dropped. Db2 keeps the alias, and using it gives -204 for the missing table.
  • A NEXT VALUE FOR expression names a sequence that isn't in that schema.

How to find the problem

  1. Read the name in the DSNT408I message or SQLERRMC. It is fully qualified, so you can see which schema Db2 used.
  2. Look the object up with SELECT CREATOR, NAME, TYPE FROM SYSIBM.SYSTABLES WHERE NAME = 'EMP_HIST'. TYPE shows a table (T), view (V) or alias (A), and CREATOR shows which schemas have one.
  3. For static SQL, check QUALIFIER and OWNER for the package in SYSIBM.SYSPACKAGE.
  4. For dynamic SQL, run SELECT CURRENT SQLID, CURRENT SCHEMA FROM SYSIBM.SYSDUMMY1 in the same session.
  5. Look at the bind job output. A VALIDATE(RUN) bind lists a warning for the same name, which tells you the problem was there before the program ran.

How to fix it

  • Rebind the package with the right qualifier for this environment, for example REBIND PACKAGE(PAYCOLL.PAYUPD1) QUALIFIER(PAYPROD).
  • For dynamic SQL, issue SET CURRENT SCHEMA = 'PAYPROD' before the query, or qualify the name in the statement.
  • Create the missing object if the name is right, or create an alias that points to the right table.
  • Fix a misspelled name in the program, then precompile, compile and bind again.
  • If a bind failed with -204, no new package was made. Bind again after the fix, or the new load module will get -805 at run time.

What it looks like

PAYUPD1 - OPEN CSR-HIST FAILED
DSNT408I SQLCODE = -204, ERROR:  PAYTEST.EMP_HIST IS AN UNDEFINED NAME
DSNT418I SQLSTATE   = 42704 SQLSTATE RETURN CODE

Questions

Why does the query work in SPUFI but fail in the program?

SPUFI runs dynamic SQL under your CURRENT SQLID or CURRENT SCHEMA. The program's static SQL uses the QUALIFIER it was bound with. If they differ, the same unqualified name points at different tables.

Is -204 the same as a missing column?

No. A missing column gives -206. -204 means the whole object, such as the table or view, couldn't be found.

Does Db2 for LUW use the same code?

Yes. SQL0204N with SQLSTATE 42704 means the same thing there.