简体   繁体   中英

Oracle SQL “column ambiguously defined” with `FETCH FIRST n ROWS ONLY`

I have a query SELECT A.ID, B.ID FROM A, B that works fine. As soon as I add FETCH FIRST n ROWS ONLY to it, the query fails with the error message

SQL Error [918] [42000]: ORA-00918: column ambiguously defined

As far as I understand, the error refers to an ambiguous SELECT clause and should not be caused by a FETCH FIRST n ROWS ONLY .

Do I miss something that justifies this behaviour? Or is this a bug?

I know that I can omit this behaviour when I specify an explicit column alias. I want to know, why SELECT A.ID, B.ID FROM A, B works, while SELECT A.ID, B.ID FROM A, B FETCH FIRST 10 ROWS ONLY doesn't.

The Oracle version is 12.1.0.2.0

This is documented in oracle documents :

Restrictions on the row_limiting_clause

If the select list contains columns with identical names and you specify the row_limiting_clause, then an ORA-00918 error occurs. This error occurs whether the identically named columns are in the same table or in different tables. You can work around this issue by specifying unique column aliases for the identically named columns.

Even though SELECT query works, after using FETCH FIRST|NEXT , it will throw error if two of the column names are same.

You should just assign different alias names for all columns in SELECT clause.

Learn to use proper, explicit, **standard* JOIN syntax. Never use commas in the FROM clause. But that is not your problem.

You have two columns with the same alias. Simply use as to assign new aliases:

SELECT A.ID as a_id, B.ID as b_id

What you are observing may be a bug. The code seems to work on other versions of Oracle.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM