Skip to main content

The columns in a SELECT should be prefixed with table name

ZPA rule for PL/SQL and Oracle SQL · ColumnsShouldHaveTableName

In a query over two or more tables, a column written without its table or alias depends on the columns of every table in the FROM clause. The query works while only one of them has a column of that name. Once a column of the same name is added to another of the tables, Oracle rejects the query with ORA-00918 ("column ambiguously defined"), and a PL/SQL unit that holds it becomes invalid. The reader also has to know every table's columns to tell where the value comes from.

This rule reports a column of the select list written without a table or alias, in a query whose FROM clause reads two or more tables, views or subqueries, separated by commas or joined with any JOIN or APPLY. A column of a JOIN … USING list is not reported: Oracle rejects a qualifier on it (ORA-25154). A query with a NATURAL JOIN is not checked: Oracle rejects a qualifier on the columns it shares (ORA-25155), and which they are depends on the columns of both sources. A name a PL/SQL variable or parameter takes, and pseudocolumns such as ROWNUM, LEVEL or SYSDATE, are not reported.

Examples​

Noncompliant Code Example
select code, -- Noncompliant
r.label
from crate c, rack r
where r.id = c.rack_id;
Compliant Solution
select c.code,
r.label
from crate c, rack r
where r.id = c.rack_id;