Oracle Troubleshoot : ORA-00918: column ambiguously defined
ORA-00918: column ambiguously defined
Cause: A column name used in a join exists in more than one table and is thus referenced ambiguously. In a join, any column name that occurs in more than one of the tables must be prefixed by its table name when referenced. The column should be referenced as TABLE.COLUMN or TABLE_ALIAS.COLUMN. For example, if tables EMP and DEPT are being joined and both contain the column DEPTNO, then all references to DEPTNO should be prefixed with the table name, as in EMP.DEPTNO or E.DEPTNO.
สำหรับ Error นี้เกิดจาก Query ที่มีการ join กัน และเรียกใช้ Column name ที่มีชื่อเดียวกันโดยไม่ได้ระบุว่าจะเรียกใช้ Column นี้จากตารางไหนนั่นเอง ดังตัวอย่าง
Example :
Table1 : Staff ( staffid, staffname )
Table2 : Staff_Detail ( staffid, staff_birthdate )
SQL> select staffid, staffname, staff_birthdate from Staff a, Staff_detail b where a.staffid = b.staffid ; |
จากตัวอย่าง column “staffid” มีอยู่ในตาราง Staff, Staff_detail ทำให้ Oracle ไม่รู้ว่า “staffid” ใน SQL จะเรียกจากตารางไหนจึงเกิด error : ORA-00918: column ambiguously defined
Action : Prefix references to column names that exist in multiple tables with either the table name or a table alias and a period (.), as in the examples above.
เพราะฉะนั้น การแก้ไขก็คือระบุชื่อตาราง หรือ alias name ของตารางที่จะเรียกใช้ column นั้นๆ เช่นจากตัวอย่างแก้ไขใหม่ได้ดังนี้
SQL> select a.staffid, a.staffname, b.staff_birthdate from Staff a, Staff_detail b where a.staffid = b.staffid ; |