在Oracle上使用内连接更新语句

我有一个在MySQL中正常工作的查询,但是当我在Oracle上运行它时,出现以下错误:

SQL错误:ORA-00933:SQL命令未正确结束
00933. 00000 – “SQL命令未正确结束”

查询是:

UPDATE table1 INNER JOIN table2 ON table1.value = table2.DESC SET table1.value = table2.CODE WHERE table1.UPDATETYPE='blah'; 

该语法在Oracle中无效。 你可以这样做:

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

或者你可以这样做:

 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 

(这取决于内联视图是否被Oracle认为是可更新的)。

Oracle不支持UPDATE语句中的连接。

用这个:

 MERGE INTO table1 trg USING ( SELECT t1.rowid AS rid, t2.code FROM table1 t1 JOIN table2 t2 ON table1.value = table2.DESC WHERE table1.UPDATETYPE='blah' ) src ON (trg.rowid = src.rid) WHEN MATCHED THEN UPDATE SET trg.value = code; 

合并where子句为我工作:

 merge into table1 using table2 on (table1.id = table2.id) when matched then update set table1.startdate = table2.start_date where table1.startdate > table2.start_date; 

您需要WHERE子句,因为ON子句中引用的列不能更新。

  UPDATE ( SELECT t1.value, t2.CODE FROM table1 t1 INNER JOIN table2 t2 ON t1.Value = t2.DESC WHERE t1.UPDATETYPE='blah') SET t1.Value= t2.CODE 

如此处所示,Tony Andrews提出的第一个解决scheme的一般语法是:

 update some_table s set (s.col1, s.col2) = (select x.col1, x.col2 from other_table x where x.key_value = s.key_value ) where exists (select 1 from other_table x where x.key_value = s.key_value ) 

我觉得这很有趣,特别是如果你想更新多个领域。

它工作得很好甲骨文

 merge into table1 t1 using (select * from table2) t2 on (t1.empid = t2.empid) when matched then update set t1.salary = t2.salary 

这下面的语法为我工作。

 UPDATE (SELECT A.utl_id, b.utl1_id FROM trb_pi_joint A JOIN trb_tpr B ON A.tp_id=B.tp_id Where A.pij_type=2 and a.utl_id is null ) SET utl_id=utl1_id; 

对table2使用描述而不是desc,

 update table1 set value = (select code from table2 where description = table1.value) where exists (select 1 from table2 where description = table1.value) and table1.updatetype = 'blah' ; 

不要使用上面的一些答案。

有人build议使用嵌套的SELECT,不要这样做,这是令人难以忍受的慢。 如果你有很多logging要更新,请使用join,如下所示:

 update (select bonus from employee_bonus b inner join employees e on b.employee_id = e.employee_id where e.bonus_eligible = 'N') t set t.bonus = 0; 

看到这个链接了解更多细节。 http://geekswithblogs.net/WillSmith/archive/2008/06/18/oracle-update-with-join-again.aspx

另外,请确保您join的所有表上都有主键。

 UPDATE table1 t1 SET t1.value = (select t2.CODE from table2 t2 where t1.value = t2.DESC) WHERE t1.UPDATETYPE='blah'; 

update table1 a set a.col1 ='Y'where exists(从table2中select1,其中a.col1 = b.col1和a.col2 = b.col2)

 UPDATE IP_ADMISSION_REQUEST ip1 SET IP1.WRIST_BAND_PRINT_STATUS=0 WHERE IP1.IP_ADM_REQ_ID = (SELECT IP.IP_ADM_REQ_ID FROM IP_ADMISSION_REQUEST ip INNER JOIN VISIT v ON ip.ip_visit_id=v.visit_id AND v.pat_id =3702 ); `enter code here` 

UPDATE(SELECT T.FIELD A,S.FIELD B FROM TABLE_T T INNER JOIN TABLE_S S ON T.ID = S.ID)SET B = A;

A和B是别名字段,您不需要指向表。