我有一个查询,在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';

当前回答

下面的语法适合我。

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;

其他回答

甲骨文基地在这方面做得很好。

https://oracle-base.com/articles/misc/updates-based-on-queries

从这个链接-我使用了上述查询的修改,这对我来说没有作用(答案来自mathguy,它使用rowid)

MERGE /*+ APPEND PARALLEL(8) */ INTO dest_table tt
USING source_table st
ON (tt.identifier = st.identifier)
WHEN MATCHED THEN
  UPDATE SET tt.number = st.number;

这里我有两个表:source和dest,它们都有一个共同的varchar字段,我将源标识字段(PK)添加到dest表中。

如这里所示,Tony Andrews提出的第一个解决方案的通用语法是:

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
                         )

我认为这很有趣,特别是当你想要更新多个字段时。

对table2使用description而不是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'
;

用这个:

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;
update table1  a 
   set a.col1='Y' 
 where exists(select 1 
                from table2 b
               where a.col1=b.col1 
                 and a.col2=b.col2
             )