SQL Reference

Column Name Qualifiers to Avoid Ambiguity

In the context of a function, a GROUP BY clause, ORDER BY clause, an expression, or a search condition, a column name refers to values of a column in some table, view, nickname, nested table expression or table function. The tables, views, nicknames, nested table expressions and table functions that might contain the column are called the object tables of the context. Two or more object tables might contain columns with the same name; one reason for qualifying a column name is to designate the table from which the column comes. Qualifiers for column names are also useful in SQL procedures to distinguish column names from SQL variable names used in SQL statements.

A nested table expression or table function will consider table-references that precede it in the FROM clause as object tables. The table-references that follow are not considered as object tables.

Table Designators

A qualifier that designates a specific object table is called a table designator. The clause that identifies the object tables also establishes the table designators for them. For example, the object tables of an expression in a SELECT clause are named in the FROM clause that follows it:

  SELECT CORZ.COLA, OWNY.MYTABLE.COLA
    FROM OWNX.MYTABLE CORZ, OWNY.MYTABLE

Table designators in the FROM clause are established as follows:

Each table designator should be unique within a particular FROM clause to avoid the possibility of ambiguous references to columns.

Avoiding Undefined or Ambiguous References

When a column name refers to values of a column, exactly one object table must include a column with that name. The following situations are considered errors:

Avoid ambiguous references by qualifying a column name with a uniquely defined table designator. If the column is contained in several object tables with different names, the table names can be used as designators. Ambiguous references can also be avoided without the use of the table designator by giving unique names to the columns of one of the object tables using the column name list following the correlation name.

When qualifying a column with the exposed table name form of a table designator, either the qualified or unqualified form of the exposed table name may be used. However, the qualifier used and the table used must be the same after fully qualifying the table name, view name or nickname and the table designator.

  1. If the authorization ID of the statement is CORPDATA:
      SELECT CORPDATA.EMPLOYEE.WORKDEPT
        FROM EMPLOYEE
    

    is a valid statement.

  2. If the authorization ID of the statement is REGION:
      SELECT CORPDATA.EMPLOYEE.WORKDEPT         
        FROM EMPLOYEE                           * incorrect *
    

    is invalid, because EMPLOYEE represents the table REGION.EMPLOYEE, but the qualifier for WORKDEPT represents a different table, CORPDATA.EMPLOYEE.


[ Top of Page | Previous Page | Next Page ]