Resolution of global variable references

Global variable reference resolution depends on whether a global variable name is qualified and where the global variable is referenced.

The order of resolving a global variable reference in relation to the names for a column, SQL variable, SQL parameter, or row variable field is described in References to SQL parameters, SQL variables, and global variables.

The implicit qualification of an unqualified global variable name that is used as the main object of a CREATE, ALTER, COMMENT, DROP, GRANT, or REVOKE statement is described in Unqualified user-defined type, function, procedure, specific, global variable and module names.

A best practice is to fully qualify the name of the global variable when referencing the global variable in an SQL statement. This prevents a subsequent change in the SQL path from having an impact on the resolution of the global variable.

The resolution of a global variable reference by the database manager in all other contexts depends on whether the global variable name is qualified.

Qualified names

To resolve the name of a qualified global variable, the reference is evaluated according to the following process:

  1. If the global variable reference is made from within a module and the qualifier matches the name of the module, the module is searched for a matching module global variable. The following rules are applied:
    • If the qualifier is a single identifier, the schema name of the module is ignored when the qualifier is compared to the module name.
    • If the qualifier is a two-part identifier, it is compared to the schema-qualified module name.
    If the name of a module global variable matches the unqualified global variable name in the reference, resolution is complete. If the qualifier does not match the name of the module or there is no matching module global variable, resolution continues with the next step.
  2. The qualifier is now considered to be a schema name. That specified schema is searched for a matching schema global variable.
    • If a schema global variable name matches the unqualified global variable name in the reference, resolution is complete.
    • If the schema does not exist, then an error is returned.
    • If there are no matching schema global variables in the schema, and the qualifier matched the name of the module in the first step, then an error is returned.
    • Otherwise, resolution continues with the next step.
  3. The qualifier is now considered to be a module name. The following rules are applied:
    • If the module name is qualified with a schema name, the module is searched for a matching published module global variable.
    • If the module name is not qualified with a schema name, the schema for the module is the first schema in the SQL path that has a matching module name. If the name of a module matches the schema name that is found in the SQL path, that module is searched for a matching published module global variable.
    • If the module is not found through the SQL path, the existence of a module public alias that matches the name of the global variable qualifier is considered. If a module public alias is found, the module that is associated with the module public alias is searched for a matching published module global variable.
    If the name of a published module global variable matches the unqualified global variable name in the global variable reference, resolution is complete. If a matching module is not found or there is no matching module global variable in the matching module, an error is returned.

Unqualified names

To resolve the name of an unqualified global variable, the reference is evaluated according to the following process:

  1. If an unqualified global variable reference is made from within a module, the module is searched for a matching module global variable. If a module global variable name matches the global variable name in the reference, resolution is complete. If there is no matching module global variable, resolution continues with the next step.
  2. The schemas in the SQL path are searched in order from left to right for a matching schema global variable. If a schema global variable name matches the global variable name in the reference, resolution is complete. If no matching global variable is found after completing this step, an error is returned.