Oracle SQL Join Eror - sql

This is my query. But I am getting following error.
Select customer_info.CUSTOMER_ID, customer_info.NAME, Customer_Hierarchy_API.Get_Description(Cust_Hierarchy_Struct_API.Get_Hierarchy_Id(CUSTOMER_ID))
from CUST_ORD_CUSTOMER_ENT
JOIN customer_info ON
customer_info.customer_id = cust_ord_customer_ent.customer_id;
ORA-00918: column ambiguously defined
00918. 00000 - "column ambiguously defined"
*Cause:
*Action: Error at Line: 1 Column: 137

Cust_Hierarchy_Struct_API.Get_Hierarchy_Id(CUSTOMER_ID)
You need to specify which table to take the CUSTOMER_ID column from, as that column name exists in two differnt tables being used (Oracle won't do it for you, programming languages hate ambiguity, so should you).
In your case it doesn't matter which, but you have to choose one.
Cust_Hierarchy_Struct_API.Get_Hierarchy_Id(customer_info.CUSTOMER_ID)
As a principle, never refer to a column on its own, always fully qualify it with the table name or alias. If when you can, don't; it's sloppy and leads to problems as/when the code or database strucutre evolves.

Another solution is the USING clause:
Select ci.CUSTOMER_ID, ci.NAME,
Customer_Hierarchy_API.Get_Description(Cust_Hierarchy_Struct_API.Get_Hierarchy_Id(CUSTOMER_ID))
from CUST_ORD_CUSTOMER_ENT coc join
customer_info ci
using (customer_id);
USING allows you to mention the join keys without having to qualify the reference. After all, the value is the same regardless of which table the value comes from, based on the JOIN condition.

Related

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.

Invalid Identifier/Ambiguously Defined

I have a SQL statement which ends in:
where <table_name>.<column_name> = '<column_value>'
I get the following error:
ORA-00904: "table_name"."column_name": invalid identifier
However, I know that the column is valid for sure. I also tried:
where <schema><table_name>.<column_name> = '<column_value>'
but got the same error. Lastly I tried without the identifiers:
where <column_name> = '<column_value>'
but that results in an column is ambiguously defined error.
What am I missing here?
Whole Query:
SELECT r.<COLUMN_NAME_1>, r.<COLUMN_NAME_2>, etc, t_append.*
FROM (
SELECT <COLUMN_NAME_1>, r.<COLUMN_NAME_2>, etc..
FROM <TABLE_NAME> ) r
inner join <TABLE_NAME> t_append on
t_append.<COLUMN_NAME_1> = r.<COLUMN_NAME_1>
AND t_append.<COLUMN_NAME_2> = r.<COLUMN_NAME_2>
AND etc...
WHERE <TABLE_NAME>.<COLUMN_NAME_1> = '<COLUMN_VALUE1>'
AND <TABLE_NAME>.<COLUMN_NAME_2> = '<COLUMN_VALUE2>'
This query takes composite key columns and value and then returns the composite key values followed by the row data which the key represents.
Apart from the above mentioned suggesstions,there may two possibilities according to me. You may get
ORA-00904: "table_name"."column_name": invalid identifier
1) if you don't have necessary permissions on the accessing objects. (confirm your permission on the object)
2) if your column was defined with double quotes like below
create table test("CheckMyColumn" number));
then it will be case sensitive. (Refer the table definition and try with same case)
The reason of a column ambiguously is because oracle doesn't know which column you are referring , it seems in your query you have specify 2 tables( from tab1 , tab2 ).
As for "table_name"."column_name": invalid identifier it means for sure column_name column for table table name doesn't exists, can you provide the ddl of the table.

SQL Queries "00904. 00000 - "%s: invalid identifier"

Hi I have the following code
SELECT entertainer_id,
entertainer_groupname
FROM casestudy_entertainer
INNER JOIN casestudy_availability ON
casestudy_entertainer.entertainer_id
= CASESTUDY_AVAILABILITY.AVAILABILITY_ENTERTAINERID
INNER JOIN casestudy_calendardates ON
CASESTUDY_AVAILABILITY.AVAILIBILITY_CALENDARDATEID
= casestudy_calendardates.calendar_Id
WHERE entertainer_type = '&Entertainer_TYPE'
AND casestudy_calendardates.calendar_date = '&Event_date'
And I don't seem to be able to figure out what its not liking when I run this.
It gives me the following error
ORA-00904: "CASESTUDY_AVAILIBILITY"."AVAILIBILITY_CALENDARDATEID": invalid identifier
00904. 00000 - "%s: invalid identifier"
*Cause:
*Action:
Error at Line: 7 Column: 4
I do have all the tables in place with all the correct rows.
The only thing is I have no data as of yet, Could this possibly be the issue?
You should try the lower case for the table/column identifiers(like in from/inner join clauses):
SELECT entertainer_id,
entertainer_groupname
FROM casestudy_entertainer
INNER JOIN casestudy_availability ON casestudy_entertainer.entertainer_id = casestudy_availability.availability_entertainerid
INNER JOIN casestudy_calendardates ON casestudy_availability.availibility_calendardateid = casestudy_calendardates.calendar_id
WHERE entertainer_type = '&Entertainer_TYPE'
AND casestudy_calendardates.calendar_date = '&Event_date'
This Error is caused by a special character in one of the columns of the database table. DBA will be able to help you.
You have same tablenames in your tables while left joining.
Change tablename of one , it will work.
no data is not the issue, you won't simply get a null result. ORA-00904 indicates, that you used a column that does not exist or does not comply to the Oracle specification.
Please check for correct naming. You might be better of with shorter names or at least table aliasses to get to code more readable.
Without knowing your table structure I cannot tell you where the error is. do a describe table_name;
It would also help to have the oracle version number SELECT * FROM V$VERSION or SELECT version FROM V$INSTANCE
and your client software you are using
What is interesting, Oracle gives that message not only if the name of the column is bad, but also if the name of the table/alias is bad - not defined alias, twice defined (as mentioned #PrajwalRai), error in the table name.
Old question, but maybe it helps:
sometimes you copy and paste text from a source that has a different character set ...
Type it over in sql-dev for instance and see if the error is gone
I was able to clear this error in an Oracle DB using Oracle SQL Developer by placing strings in single quotes instead of double quotes

Bringing the other column , along all with all the other columns of the table (Oracle)

I am trying to Bringing the other column , along all with all the other columns of the table (Oracle)
As shown below
select order_id,person_id,col4,col5,* from orders
It is giving the below error :
ORA-00936: missing expression
00936. 00000 - "missing expression"
*Cause:
*Action:
Error at Line: 1 Column: 66
Any inputs would be helpful !!
Use an alias:
select col1, col2, c.*
from my_table c
You can't specify columns together * wild card in Oracle, try if possible with alias
select order_id as A,person_id as B,col4 as C,col5 as D,* from orders
You cannot specify columns and the use a wildcard on the same query when doing a SQL SELECT.
This is due to projections and (as in relational algebra) projections requires that you specify attributes for selection.
In order to fully use projections, you either set attribute names and use alias (if you want to use wildcards).

error with a sql query because of ambiguous column name

I'm trying to create a sql query, but there is this error:
Ambiguous column name 'description'.
Its because this column occurs in both tables.
if I remove the description from the query, it works.
I tried to rename the description-field "AS description_pointer", but the error still occurs.
SELECT TOP 1000 [activityid]
,[activitytypecodename]
,[subject]
,[regardingobjectid]
,[contactid]
,[new_crmid]
,[description] AS description_pointer
FROM [crmtestext_MSCRM].[dbo].[FilteredActivityPointer] as I
Left JOIN [crmtestext_MSCRM].[dbo].[FilteredContact]
ON I.[regardingobjectid] = [crmtestext_MSCRM].[dbo].[FilteredContact].[contactid]
WHERE new_crmid not like '%Null%' AND activitytypecodename like '%E-mail%'
Both tables coming into play in the query have a column named description. You RDBMS cannot guess which column table you actually want.
You need to prefix the column name with the table name (or table alias) to disambiguate it.
Bottom line, it is a good practice to always prefix column names with table names or aliases as soon as several tables come into play in a query. This avoids the issue that you are seeing here and make the queries easier to understand for the poor souls that have no knowledge of the underlying schema.
Here is an updated version of your query with table aliases and column prefixes. Obviously you need to review each column to put the correct alias:
SELECT TOP 1000
i.[activityid]
,i.[activitytypecodename]
,i.[subject]
,c.[regardingobjectid]
,c.[contactid]
,c.[new_crmid]
,c.[description] AS description_pointer
FROM [crmtestext_MSCRM].[dbo].[FilteredActivityPointer] as i
Left JOIN [crmtestext_MSCRM].[dbo].[FilteredContact] as c
ON i.[regardingobjectid] = c.[contactid]
WHERE i.new_crmid not like '%Null%' AND i.activitytypecodename like '%E-mail%'