So I have the "column ambiguously defined" error in my sql oracle code and I can't figure out why I have the error. I know it means the code can't figure out which of two columns with the same name to use, so I need to use prefixes. But I have prefixes that are all correct. I looked at the other questions on this error message on the site, but can't figure it out.
ORA-00918: kolumn inte entydigt definierad
00918. 00000 - "column ambiguously defined"
*Cause:
*Action:
Error at Line: 4 Column: 5
SELECT KUND.KNR, KUND.FNAMN, KUND.ENAMN
FROM KUND, ORDERRAD, KUNDORDER, KUNDORDER, ARTIKEL, VARUGRUPP
WHERE KUND.KNR = KUNDORDER.KNR
AND KUNDORDER.ORDNR = ORDERRAD.ORDNR
AND ORDERRAD.ARTNR = ARTIKEL.ARTNR
AND ARTIKEL.VGNR = VARUGRUPP.VGNR
AND VARUGRUPP.VGNAMN = 'skäggvård' OR VARUGRUPP.VGNAMN = 'bondgård';
You join the table KUNDORDER twice, so you have to use an alias to help oracle distinguish between them.
But looking at this, most likely you meant to join this table once, and it is a typo.
As a side note: you're using deprecated implicit join notation: it's better to use FROM tablename JOIN table1 ON (...) JOIN table2 ON (...)-notation.
Second side note: it's good practice to always use aliases for your joined tables to improve readability, e.g.: FROM table1 t1 JOIN table2 t2 ON (...)
Related
I have to run the following UPDATE query into an Oracle database.
UPDATE Appliance
SET Appliance.IdApplianceType =
(
SELECT AT.id
FROM Appliance A INNER JOIN ApplianceType AT
ON UPPER(A.typeName) = UPPER(AT.name)
AND Appliance.id = A.id)
The objective is to find the match between records of Appliance.typeName and ApplianceType.name and set the ApplianceType.id (primary key) in the Appliance.IdApplianceType (FK ApplianceType) (Note: In a 2nd step normalize Appliance table removing Appliance.typeName column and to use IdApplianceType like relation.)
In oracle 12c(and sqlserver 2008+) it works while in version 11g doesn't work.
I report the error below
QL Error: ORA-00904: "APPLIANCE"."ID": invalid identifier
00904. 00000 - "%s: invalid identifier"
*Cause:
*Action:
Any help will be appreciated :)
Thanks
The extra join seems unnecessary. Why not just use this simpler version?
UPDATE Appliance
SET Appliance.IdApplianceType =
(SELECT AT.id
FROM ApplianceType AT
WHERE UPPER(Appliance.typeName) = UPPER(AT.name)
);
Trying to join two tables but am getting an error
SELECT
FROM ods.academic_outcome_pu
INNER JOIN ods.academic_outcome_pu
ON ods.academic_study.academic_period=ods.academic_outcome_pu.academic_period
AND ON ods.academic_study.person_uid=ods.academic_outcome_pu.person_uid
AND ON ods.academic_study.program=ods.academic_outcome_pu.program
Error returned:
ERROR: syntax error at or near "FROM"
LINE 2: FROM ods.academic_outcome_pu
SQL state: 42601
Character: 8
You're trying to join two tables, but repeated the same table in the FROM-list. From the aliases, it seems you have ods.academic_study and ods.academic_outcome_pu tables, and had better using aliases instead of explicitly writing the table names everytime to qualify the columns stated for the join conditions. And keyword ON should be used only once.
Btw, your error raises due to missing columns next to SELECT keyword. ( col[1/2] are just presumed column names which should be replaced by the real column names ) :
SELECT ods.col1, ods.col2, aop.col1, aop.col2
FROM ods.academic_study ast
JOIN ods.academic_outcome_pu aop
ON ast.academic_period = aop.academic_period
AND ast.person_uid = aop.person_uid -- this first "AND" can be replaced by "WHERE" which could be used once also.
AND ast.program = aop.program
I am trying to execute this query but I am getting the following error:
ORA-00905: missing keyword
00905. 00000 - "missing keyword"
*Cause:
*Action:
Error at Line: 25 Column: 51
The query is:
SELECT egt.education_guarantee_type_id, egt.description, egt.is_available, egy.year_number
FROM mo_education_guarantee_types egt
INNER JOIN mo_education_guarantee_years egy;
What keyword is missing from my query?
You are close but as jarlh said in his comment you need a join condition such as
SELECT egt.education_guarantee_type_id, egt.description, egt.is_available, egy.year_number
FROM mo_education_guarantee_types egt
INNER JOIN mo_education_guarantee_years egy ON egt.SOMEKEY = egy.SOMEKEY;
SOMEKEY here will refer to a field that exists in both tables.
Hope that helps.
Can anyone help me with the following statement. I'm new to SQL and maybe overlooking the obvious.
I am trying to run the following query
There is a shipments table with the corresponding columns (PROJECTNO, SUPPLIERNO)
I know I don't the to put SHIPMENTS. in front of the names, but this will eventually end up in a multi table query.
SELECT sup1.SHIPMENTS.PROJECTNO, sup2.SHIPMENTS.PROJECTNO
FROM SHIPMENTS sup1, SHIPMENTS sup2
WHERE sup1.SHIPMENTS.PROJECTNO = sup2.SHIPMENTS.PROJECTNO
AND sup1.SHIPMENTS.SUPPLIERNO <> sup2.SHIPMENTS.SUPPLIERNO;
I keep getting the following error.
ORA-00904: "SUP2"."SHIPMENTS"."SUPPLIERNO": invalid identifier
00904. 00000 - "%s: invalid identifier"
*Cause:
*Action:
Error at Line: 160 Column: 34
THanks in advance
Your query should be written as:
SELECT sup1.PROJECTNO, sup2.PROJECTNO
FROM SHIPMENTS sup1 JOIN
SHIPMENTS sup2
on sup1.PROJECTNO = sup2.SHIPMENTS.PROJECTNO AND
sup1.SUPPLIERNO <> sup2.SUPPLIERNO;
First, when using an alias there is no need to specify the table name again. Also, you should use explicit join syntax, with an on condition. The implicit syntax, with the condition in the where clause, is supported, but it is less powerful and more difficult to read.
Change all sup2.SHIPMENTS.SUPPLIERNO to sup2.SUPPLIERNO, and do the same for sup1 alias, and for the other colummns.
You must understand aliasing. Once you alias table TAB as T then you should refer to it as simply T.
In your FROM clause, when you say
FROM SHIPMENTS sup1, SHIPMENTS sup2
you are aliasing the SHIPMENTS table as sup1 and sup2 (as 2 instances of same table). So you need to use sup1 and sup2 as the table name, and the real name (SHIPMENTS) isn't valid, and would be ambiguous anyway, so use:
sup1.SUPPLIERNO --refers to SUPPLIERNO column in SHIPMENTS table aliased as sup1
sup2.PRODUCTNO --refers to PRODUCTNO column in SHIPMENTS table aliased as sup2
there is no such thing as sup2.SHIPMENTS.SUPPLIERNO because there is no such thing as a sup2.SHIPMENTS table
Also, a general rule to keep in mind, in Oracle you can use notation like table.column, schema.table or schema.table.column to refer to tables/views or columns. In this case what you've written amounts to table.table.column which doesn't make sense (unless you were using a nested table).
The reason you must use aliasing here is that it is the only way to join a table to itself. If you use the full table name twice, you'll get ambiguity errors. When you self-join a table, Oracle treats each instance (alias) as a distinct table in the query. I prefer to use aliases like a and b to reduce typos.
SELECT a.PROJECTNO, b.PROJECTNO
FROM SHIPMENTS a JOIN SHIPMENTS b USING(PROJECTNO)
WHERE a.SUPPLIERNO <> b.SUPPLIERNO;
I am asking this question because I am not getting an error where I expect there should be an error.
Please help me understand under what circumstances this is possible. I have a query:
select foracid,acct_name, schm_code, schm_type from tbaadm.gam where
acid in(select acid from tbaadm.iar);
This query is returning results without throwing any error. I expect invalid identifier
because the table tbaadm.iar does NOT have a field acid.
When I run:
select acid from tbaadm.iar;
I get:
ORA-00904: "ACID": invalid identifier
00904. 00000 - "%s: invalid identifier"
*Cause:
*Action:
Error at Line: 772 Column: 8
I am using sqldeveloper and oracle 10g. It is kind of Strange to me. Worth to mention though is that there is a field in tbaadm.iar that is an id and so the right Query Should be:
select foracid,acct_name, schm_code, schm_type from tbaadm.gam where
acid in(select entity_id from tbaadm.iar);
What is going on here?
A subquery that's used in an IN clause can reference columns from the outer query, because this is necessary in correlated subqueries. So your WHERE clause is equivalent to:
WHERE acid IN (SELECT tbaadm.gam.acid FROM tbaadm.iar)
An example of a correlated subquery that shows why this is necessary is:
SELECT *
FROM outer_table
WHERE somefield = (SELECT someotherfield
FROM inner_table
WHERE inner_table.id = outer_table.inner_id)
This is the more common use, where the field from the outer table is used in a WHERE clause of the subquery. But SQL isn't picky about where the field from the outer query is used. It can be used anywhere in the subquery that an expression is permitted, which includes the SELECT clause.