Column name qualifiers to avoid ambiguity
In
the context of a function, a GROUP BY clause, an ORDER BY clause, an expression, or a search
condition, a column name refers to values of a column in some table or view in a DELETE, MERGE, or
UPDATE statement or table-reference in a FROM clause.
The tables, views, and table-references1 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 object from which the column comes. For information on avoiding ambiguity between SQL parameters and variables and column names, see References to SQL parameters and SQL variables in external SQL procedures.
A nested table expression which is preceded by a TABLE keyword will consider table-references that precede it in the FROM clause as object tables. The table-references that follow it are not considered as object tables.
SELECT DISTINCT Z.EMPNO, EMPTIME, PHONENO
FROM DSN8C10.EMP Z, DSN8C10.EMPPROJACT
WHERE WORKDEPT = 'D11'
AND EMPTIME > 0.5
AND Z.EMPNO = DSN8C10.EMPPROJACT.EMPNO;Table designators in the FROM clause are established as follows:
- A name that follows a table or view name is both a correlation name and a table designator. Thus, Z is a table designator and qualifies the first column name in the select list.
- An exposed table or view name is a table designator. Thus, the qualified table name, DSN8C10.EMPPROJACT is a table designator and qualifies the second column name in the select list.
SELECT *
FROM DSN8C10.EMP X, DSN8C10.EMP Y;Avoiding undefined or ambiguous references in Db2 SQL: When a column name refers to values of a column, the following situations result in errors:
- No object table contains a column with the specified name. The reference is undefined.
- The column name is qualified by a table designator, but the table named does not include a column with the specified name. Again, the reference is undefined.
- The name is unqualified and more than one object table includes a column with that name. The reference is ambiguous.
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.
Two or more object tables can be instances of the same table. A FROM clause that includes n references to the same table should include at least n - 1 unique correlation names.
SELECT X.LASTNAME, Y.LASTNAME
FROM DSN8C10.EMP X, DSN8C10.EMP Y
WHERE Y.JOB = 'MANAGER'
AND X.WORKDEPT = Y.WORKDEPT
AND X.JOB <> 'MANAGER';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 can be used. However, the qualifier used and the table used must be the same after fully qualifying the table name or view name and the table designator.
SELECT CORPDATA.EMPLOYEE.WORKDEPT
FROM EMPLOYEE; SELECT CORPDATA.EMPLOYEE.WORKDEPT -- Incorrect
FROM EMPLOYEE; SELECT EMPLOYEE.WORKDEPT -- Incorrect
FROM CORPDATA.EMPLOYEE;