Oracle 11g Update statement with Error ORA-00904 - sql

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)
);

Related

How to copy column from one table to another which can be joined by ID?

UPDATE s
SET s.TECH_NAME = r1.TECH_NAME
FROM **ERRMSG** s INNER JOIN **RAWDATA** r1 on
s.id = r1.id;
I want to update tech_name in table ERRMSG from table RAWDATA.Joining condition is ID.Whats wrong in the above query.Im getting the following error
Error at Command Line : 62 Column : 1
Error report -
SQL Error: ORA-00933: SQL command not properly ended
00933. 00000 - "SQL command not properly ended"
*Cause:
*Action:
This sounds like a job for the MERGE statement documented here:
https://docs.oracle.com/cd/B28359_01/server.111/b28286/statements_9016.htm
This is more in the spirit of what you were trying to do, it avoids correlated subqueries, and it gives you more flexibility.
merge into errmsg s
using rawdata r1
on (s.id = r1.id)
when matched then update set s.tech_name = r1.tech_name
If needed, you can use the other capabilities of MERGE as well; for example, when NOT matched, the id in the errmsg table does not have a corresponding id in the rawdata table. Would you like to take an action in that case as well? You could, for example, set the tech_name to NULL or some default value or delete the row altogether - you can do all these things in one MERGE statement. See the documentation I provided.
What is wrong is that you are using syntax that Oracle does not support. Use a correlated subquery instead:
UPDATE errmsg s
SET s.TECH_NAME = (SELECT r1.TECH_NAME FROM rawdata r1 WHERE s.id = r1.id)
WHERE EXISTS (SELECT 1 FROM rawdata r1 WHERE s.id = r1.id)

SQL 00918. 00000 - "column ambiguously defined"

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 (...)

Missing keyword in Oracle select query

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.

sql not throwing invalid identifier

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.

Deleting from multiple tables 5

I am having trouble deleting from multiple tables.
I am using the code below to delete from multiple tables:
DELETE
FROM usession,
upklist,
projshar USING usession
LEFT JOIN upklist
ON upklist.session_id = usession.session_id
LEFT JOIN Projshar
ON projshar.session_id = usession.session_id
WHERE usession.session_id =
(SELECT session_id
FROM USESSION
WHERE delete_session_id IS NULL
AND user_id =
(SELECT user_id FROM users WHERE regexp_like(USER_NAME, 'gfcashmo', 'i')
)
);
I am using sql developer connection to an oracle database and get the following error
which references the second line - FROM usession,
Error at Command Line:274 Column:13 Error report: SQL Error:
ORA-00933: SQL command not properly ended
00933. 00000 - "SQL command not properly ended"
*Cause:
*Action:
According to ducumentation of DELETE statement Oracle has no support for deleting from multiple tables.