Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update statement with inner join on Oracle

I have a query which works fine in MySQL, but when I run it on Oracle I get the following error:

SQL Error: ORA-00933: SQL command not properly ended
00933. 00000 - "SQL command not properly ended"

The query is:

UPDATE table1 INNER JOIN table2 ON table1.value = table2.DESC SET table1.value = table2.CODE WHERE table1.UPDATETYPE='blah'; 
like image 304
user169743 Avatar asked Mar 15 '10 11:03

user169743


People also ask

Can we use inner join in update statement in Oracle?

The answer is pretty straightforward: in Oracle this syntax of UPDATE statement with a JOIN is not supported.

Can you have a join in an update statement?

An UPDATE statement can include JOIN operations. An UPDATE can contain zero, one, or multiple JOIN operations. The UPDATE affects records that satisfy the JOIN conditions.

Can we use inner join in update statement MySQL?

The MySQL Update Join is used for executing the update statement together with the implementation of INNER JOIN and LEFT JOIN MySQL clauses in the server. This Update JOIN clause in MySQL helps to retrieve the data records from the related database tables along with modifying them with the query.


1 Answers

That syntax isn't valid in Oracle. You can do this:

UPDATE table1 SET table1.value = (SELECT table2.CODE                                   FROM table2                                    WHERE table1.value = table2.DESC) WHERE table1.UPDATETYPE='blah' AND EXISTS (SELECT table2.CODE             FROM table2              WHERE table1.value = table2.DESC); 

Or you might be able to do this:

UPDATE  (SELECT table1.value as OLD, table2.CODE as NEW  FROM table1  INNER JOIN table2  ON table1.value = table2.DESC  WHERE table1.UPDATETYPE='blah' ) t SET t.OLD = t.NEW 

It depends if the inline view is considered updateable by Oracle ( To be updatable for the second statement depends on some rules listed here ).

like image 92
Tony Andrews Avatar answered Sep 28 '22 10:09

Tony Andrews