I have to execute a SQL made from some users and show its results. An example SQL could be this:
SELECT t1.*, t2.* FROM table1 t1, table2 t2, where table1.id = table2.id
This SQL works fine as it is, but I need to manually add pagination and show the rownum, so the SQL ends up like this.
SELECT z.*
FROM(
SELECT y.*, ROWNUM rn
FROM (
SELECT t1.*, t2.* FROM table1 t1, table2 t2, where table1.id = table2.id
) y
WHERE ROWNUM <= 50) z
WHERE rn > 0
This throws an exception: "ORA-00918: column ambiguously defined" because both Table1 and Table2 contains a field with the same name ("id").
What could be the best way to avoid this?
Regards.
- UPDATE
In the end, we had to go for the ugly way and parse each SQL coming before executing them. Basically, we resolved asterisks to discover what fields we needed to add, and alias every field with an unique id. This introduced a performance penalty but our client understood it was the only option given the requirements.
I will mark Lex answer as it´s the solution we ended up working on.
Use replace null values function to fix this.
If you could modify the query syntactically (or get the users to do so) to use explicit
JOIN
syntax with theUSING
clause, this would automatically fix the problem at hand:The
USING
clause does the same asON t1.id = t2.id
(or the implicitJOIN
you have in the question), except that only oneid
column remains in the result, thereby eliminating your problem.You would still run into problems if there are more columns with identical names that are not included in the
USING
clause. Aliases as described by @Lex are indispensable then.I think you have to specify aliasses for (at least one of) table1.id and table2.id. And possibly for any other corresponding columnnames as well.
So instead of
SELECT t1.*, t2.* FROM table1 t1, table2
use something like:I'm not familiar with Oracle syntax, but I think you'll get the idea.
I was searching for an answer to something similar. I was referencing an aliased sub-query that had a couple of NULL columns. I had to alias the NULL columns because I had more than one;
select a.*, t2.column, t2.column, t2.column (select t1.column, t1.column, NULL, NULL, t1.column from t1 where t1='VALUE') a left outer join t2 on t2.column=t1.column;
Once i aliased the NULL columns in the sub-query it worked fine.