Showing posts with label Db2 catalog. Show all posts
Showing posts with label Db2 catalog. Show all posts

Thursday, 9 July 2026

Db2 Schemas Guide: Qualifiers, CURRENT SCHEMA, and COBOL SQL

A COBOL program can pass bind, compile, and link-edit, then still fail at runtime with SQLCODE -204 because Db2 looked for the table under the wrong qualifier. The SQL text says SELECT * FROM EMPLOYEE, but Db2 might resolve that unqualified table name as PAYROLL.EMPLOYEE, DEV01.EMPLOYEE, or another schema depending on the execution context.

A Db2 schema is a logical owner or namespace for database objects. Tables, views, indexes, aliases, triggers, routines, sequences, and other objects live under a schema name. In production COBOL SQL, the schema is not just a catalog detail. It controls name resolution, migration safety, package promotion, and the difference between testing against the intended table and testing against a same-named object in another environment.

Db2 schema qualifier mapping tables views indexes and object names
A schema qualifies Db2 object names before SQL is executed.

Last updated: July 2026.

What a Db2 Schema Does

A schema groups objects under a qualifier. The fully qualified form is:

schema_name.object_name

For example, PAYROLL.EMPLOYEE and CLAIMS.EMPLOYEE can both exist because the schema names are different. The object name alone is the same, but the fully qualified names point to different objects.

Object reference Meaning Production risk
EMPLOYEE Unqualified table name. Db2 must infer the schema. Can resolve differently across test, QA, and production.
PAYROLL.EMPLOYEE Qualified table name. The schema is explicit. Safer for shared subsystems and promotion paths.
SET SCHEMA = 'PAYROLL' Sets the schema used for unqualified dynamic SQL names. Must be controlled in every code path that prepares SQL.

Schema Versus Database, Table Space, and Owner

Mainframe teams sometimes mix schema, database, table space, and owner because all four can appear near the same object. They are not the same thing.

  • Schema: the qualifier used in SQL object names, such as PAYROLL in PAYROLL.EMPLOYEE.
  • Database: a Db2 container for objects such as table spaces and indexes.
  • Table space: the storage structure that holds table data. See the related Db2 table spaces guide.
  • Authorization ID or owner: the ID or role associated with object creation and privileges.

A schema can look like an owner because many sites create objects under an authorization ID, but production rules should still name the concept clearly. When an abend ticket says "wrong database," but the failing SQL references an unqualified table, the first check is usually schema resolution.

How Db2 Resolves Unqualified Names

When SQL names an object without a qualifier, Db2 applies name-resolution rules. Static SQL and dynamic SQL are not always handled the same way, so a COBOL program can behave differently depending on whether a statement is embedded and bound in a package or prepared at runtime.

For dynamic SQL, IBM documents the CURRENT SCHEMA special register as the schema used to qualify unqualified object references in dynamically prepared statements. That makes CURRENT SCHEMA a real production setting, not a theory question.

SET SCHEMA = 'PAYROLL';

SELECT EMPNO, LASTNAME
  FROM EMPLOYEE
 WHERE DEPTNO = 'A00';

After the SET SCHEMA, the unqualified EMPLOYEE reference is resolved under PAYROLL for dynamic SQL. Many shops still prefer explicit qualifiers in production SQL because the qualifier is visible in the statement, the bind review, and the incident ticket.

CREATE SCHEMA Example

The basic DDL is short:

CREATE SCHEMA PAYROLL;

CREATE TABLE PAYROLL.EMPLOYEE
 (EMPNO     CHAR(6)     NOT NULL,
  LASTNAME  VARCHAR(30) NOT NULL,
  DEPTNO    CHAR(3)     NOT NULL,
  PRIMARY KEY (EMPNO));

The important part for COBOL developers is not the first line alone. It is the later discipline of using the same qualifier in DCLGEN, embedded SQL, bind jobs, catalog checks, and operations runbooks.

COBOL and Static SQL Example

A static SQL cursor in a COBOL program should make the target object clear. In regulated shops, this helps code review because the package points at the expected application schema.

EXEC SQL
    DECLARE C1 CURSOR FOR
        SELECT EMPNO, LASTNAME, DEPTNO
          FROM PAYROLL.EMPLOYEE
         WHERE DEPTNO = :WS-DEPTNO
END-EXEC.

If the program uses DCLGEN copybooks, keep the schema naming policy consistent with the copybook generation process. A DCLGEN made from PAYROLL.EMPLOYEE but used with SQL that references only EMPLOYEE can hide a promotion error until bind or execution. For the full compile and bind path, see the Db2 application environment guide.

Common SQLCODE -204 Scenario

SQLCODE -204 is often the first visible symptom of a schema problem. A batch job might fail in QA with a message that the object is undefined. The table exists, the DBA can query it, and the developer can see it in the catalog. The mismatch is usually that Db2 looked for QAUSER.EMPLOYEE while the real table is PAYROLL.EMPLOYEE.

-- Failing dynamic SQL path
SELECT EMPNO FROM EMPLOYEE;

-- Confirm intended object in the catalog
SELECT CREATOR, NAME, TYPE
  FROM SYSIBM.SYSTABLES
 WHERE NAME = 'EMPLOYEE';

Use the Db2 catalog guide to verify the object owner and type. Then check whether the SQL should use an explicit qualifier, whether SET SCHEMA is missing, or whether the package was bound under the wrong qualifier rules.

Schema Checks Before Promotion

Before moving a COBOL Db2 change from test to production, add schema checks to the same review as package, plan, and collection checks.

  • Search embedded SQL for unqualified tables, views, aliases, sequences, and routines.
  • Compare DCLGEN source against the schema used by the target package.
  • Confirm bind jobs use the expected collection and qualifier settings.
  • Run catalog queries for same-named objects under different schemas.
  • For dynamic SQL, verify any SET SCHEMA path runs before PREPARE.
  • Check SQLCODE handling so -204, -551, and related errors are logged with the resolved object name when available.

Those checks belong near the bind and deployment controls covered in the Db2 packages guide and the Db2 DSN command reference.

When to Use Explicit Qualifiers

Use explicit qualifiers when the SQL targets shared production objects, when the subsystem contains multiple application schemas, or when the statement is reviewed by operations during an incident. A qualified name gives the reviewer one less variable to infer.

Unqualified names can still be acceptable in controlled dynamic SQL frameworks where the application sets CURRENT SCHEMA deliberately and logs that setting. The rule should be written down. If developers have to guess, the next promotion will repeat the same -204 investigation.

FAQ

Is a Db2 schema the same as a database?

No. A schema is a SQL namespace or qualifier for objects. A database is a Db2 container used with storage structures such as table spaces.

Why does the same COBOL SQL work in test but fail in production?

One common reason is unqualified object names. Test might resolve EMPLOYEE under one schema while production expects PAYROLL.EMPLOYEE.

Should COBOL programs qualify every table name?

For production static SQL, explicit qualifiers are usually easier to review and troubleshoot. Some sites rely on bind or dynamic SQL settings, but the rule must be consistent.

Does SET SCHEMA affect static SQL?

Treat SET SCHEMA mainly as a dynamic SQL control. Static SQL is resolved through precompile and bind rules, so check the package and bind settings instead of assuming a runtime SET SCHEMA will fix it.

Practical Rule

When a Db2 object name can exist in more than one place, qualify it or prove which schema Db2 will use. That one check prevents many late-night SQLCODE -204 calls.

Saturday, 24 August 2013

Db2 Catalog Guide: SYSIBM Tables, Packages, Indexes, and Statistics


Db2 catalog flow from DDL to SYSIBM catalog tables and support queries
Query catalog tables before changing objects.

When a COBOL Db2 job fails with an object, privilege, or package problem, the catalog is often the fastest place to confirm the facts. It can show whether a table exists, which columns it has, which indexes support it, when statistics were collected, and which package or plan is tied to application SQL.

The Db2 catalog is a set of Db2 tables, mostly under the SYSIBM schema, that records metadata about objects, authorizations, packages, plans, constraints, routines, communications, and optimizer statistics. Db2 updates many catalog rows when DDL, DCL, bind, or utility work changes the system.

Catalog versus directory

The catalog and directory are both used by Db2, but they serve different support roles. The catalog is queryable through SQL and is useful for developers, DBAs, and support teams. The directory contains internal control information that Db2 uses to run and recover the subsystem; it is not a normal application query target.

AreaWhat it containsHow support teams use it
Db2 catalogMetadata about tables, columns, indexes, views, privileges, packages, plans, routines, constraints, and statistics.Query with SQL to investigate object definitions, authorization, bind status, and access-path inputs.
Db2 directoryInternal Db2 control information needed for operation and recovery.Managed by Db2 and DBA utilities; do not treat it as a normal reporting source.

Catalog tables developers often use

Catalog tableTypical question it answers
SYSIBM.SYSTABLESDoes this table, view, alias, or synonym exist, and who owns it?
SYSIBM.SYSCOLUMNSWhat are the column names, data types, lengths, null rules, and column order?
SYSIBM.SYSINDEXESWhich indexes exist for a table, and are they unique or clustering indexes?
SYSIBM.SYSKEYSWhich columns form an index key, and in what order?
SYSIBM.SYSTABLESPACEWhich table spaces exist, and which database owns them?
SYSIBM.SYSTABAUTHWhich table or view privileges are granted?
SYSIBM.SYSUSERAUTHWhich system-level authorities are recorded for an authorization ID?
SYSIBM.SYSPACKAGEWhich packages exist for an application, collection, or version?
SYSIBM.SYSPACKSTMTWhich SQL statements are associated with a package?
SYSIBM.SYSROUTINESWhich stored procedures and user-defined functions exist?

Catalog queries for daily support

Find columns for a table

SELECT NAME,
       COLTYPE,
       LENGTH,
       NULLS,
       COLNO
  FROM SYSIBM.SYSCOLUMNS
 WHERE TBOWNER = 'APP1'
   AND TBNAME  = 'ACCOUNT'
 ORDER BY COLNO;

This is a quick check before changing a COBOL copybook, DCLGEN member, or host variable definition.

List indexes for a table

SELECT I.NAME,
       I.CREATOR,
       I.UNIQUERULE,
       I.CLUSTERING
  FROM SYSIBM.SYSINDEXES I
 WHERE I.TBCREATOR = 'APP1'
   AND I.TBNAME    = 'ACCOUNT'
 ORDER BY I.NAME;

Use this before reviewing an access path or explaining why a predicate is not using the expected index.

Check package existence

SELECT COLLID,
       NAME,
       VERSION,
       VALID,
       OPERATIVE
  FROM SYSIBM.SYSPACKAGE
 WHERE COLLID = 'APP1COLL'
   AND NAME   = 'ACCTUPD';

This helps when a runtime failure points to a missing, invalid, or wrong collection package.

Catalog data used by the optimizer

Db2 uses catalog statistics when it chooses access paths for static and dynamic SQL. RUNSTATS updates statistics such as table cardinality, index cardinality, column distribution, and partition-level information. Stale statistics can lead Db2 to pick a poor access path even when the SQL text has not changed.

Catalog areaWhy it matters
SYSTABLES and related statistics rowsTable size and organization influence access-path choice.
SYSINDEXES and key statisticsIndex availability, uniqueness, and clustering affect predicate access.
SYSCOLDISTColumn distribution data can help Db2 estimate filter factors for skewed values.
History statistics tablesUseful for comparing recent statistics changes when a query changed behavior after maintenance.

Catalog safety rules

  • Use catalog SELECT queries for investigation; do not update catalog tables directly unless IBM documentation and site procedure explicitly allow a specific task.
  • Prefer Db2 DDL, DCL, BIND, REBIND, RUNSTATS, and utilities to make supported changes.
  • Use qualified names, especially owner, creator, database, table space, collection, and package name.
  • Check the Db2 subsystem before comparing test and production catalog rows.
  • Save catalog query output when it explains a production incident or release issue.

Common support scenarios

SQLCODE says an object is not found

Check SYSTABLES, SYSTABLESPACE, package collection, qualifier, and bind options. A table can exist in one subsystem or schema while the program is bound to another.

A query changed after RUNSTATS

Check catalog statistics and package bind time. If a rebind occurred after statistics changed, the package might have a new access path.

A user cannot run a query

Check table privileges, system privileges, role or group handling, and whether the application runs under a different authorization ID than the interactive user.

Related Db2 topics

Use this guide with Db2 Directory, Db2 Packages, Db2 Indexing, Db2 Utilities, and Db2 Optimizer.

FAQ

What is the Db2 catalog?

The Db2 catalog is a set of Db2 tables that stores metadata about objects, columns, indexes, privileges, packages, plans, routines, constraints, communications, and optimizer statistics.

Can developers query the Db2 catalog?

Yes, when they have the required authority. Catalog SELECT queries are common for checking object definitions, package status, privileges, and statistics.

Should catalog tables be updated manually?

No for normal work. Use supported Db2 statements, bind commands, utilities, and DBA procedures. Direct catalog updates are dangerous unless a documented IBM or site procedure specifically requires them.

New In-feed ads