SQLCODE -203: Ambiguous column reference
SQLCODE -203 (SQLSTATE 42702) means a column name in the statement could refer to more than one column, usually because two tables in the FROM clause both have a column with that name. Db2 won't guess which one you meant.
Other names for this code
Also written as SQL -203, SQLCODE=-203, SQLCODE = -203, db2 -203, sqlstate 42702, SQL0203N, reference to column is ambiguous.
What it means
When a query reads more than one table, Db2 matches each unqualified column name against all of them. If the name exists in only one, there is no problem. If it exists in two or more, Db2 can't tell which you mean, so it rejects the statement with -203 and names the column.
Joins on a shared key are the classic case. If ORDERS and CUSTOMER both have CUST_SELECT CUST_ or WHERE CUST_ is ambiguous, even though the two columns hold the same value after the join. Db2 doesn't know that and doesn't assume it.
A query can also become ambiguous without being changed. If a column is added to one of the tables and it has the same name as a column in another table of the join, the next bind or prepare fails with -203.
Common causes
- A join where both tables have a column with the same name, and the statement uses it unqualified.
- A self-join where a column is written without the correlation name, so Db2 can't tell which copy of the table it belongs to.
- A column added to one table that now clashes with a column in another table of an existing join.
- A nested table expression that has two columns with the same name. Even a reference qualified with its correlation name is then ambiguous.
- In a CREATE TRIGGER, a column of the triggering table is used without the OLD or NEW correlation name for the transition variable.
How to find the problem
- Take the column name from the
DSNT408Imessage or SQLERRMC. - List the tables and views in the FROM clause and check which ones have that column, for example with
SELECT TBNAME FROM SYSIBM.SYSCOLUMNS WHERE NAME = 'CUST_.NO' AND TBCREATOR = 'PAYPROD' - If the query worked before, check whether a column was recently added to one of the tables.
- For static SQL, the bind output gives the statement number in the DBRM.
How to fix it
- Qualify the column with a correlation name, as in
SELECT O.CUST_.NO FROM ORDERS O JOIN CUSTOMER C ON O.CUST_ NO = C.CUST_ NO - Give every table in a join a short correlation name, and use it on every column. Then a new column can't break the query.
- In a self-join, use different correlation names for each copy of the table.
- If a nested table expression returns two columns with the same name, rename one of them with AS so each name is unique.
- Rebind or re-prepare after the change.
What it looks like
DSNT408I SQLCODE = -203, ERROR: A REFERENCE TO COLUMN CUST_NO IS AMBIGUOUS DSNT418I SQLSTATE = 42702 SQLSTATE RETURN CODE
IBM documentation
IBM documentation: -203. Use the documentation for your installed release.
Related codes
Questions
Why is it ambiguous when both columns hold the same value?
Db2 resolves names before it looks at data. Two columns with the same name in different tables are different columns, even when the join makes their values equal.
Why did the query break when no one changed it?
A column was probably added to one of the tables with a name another table in the join already uses. The next bind or prepare then finds two matches.
Is it the same on Db2 for LUW?
Yes. SQL0203N has SQLSTATE 42702 and the same meaning.