在Oracle上使用内部联接更新语句

时间:2010-03-15 11:40:21

标签: sql oracle inner-join ora-00933

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

15 个答案:

答案 0 :(得分:364)

该语法在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更新)。

答案 1 :(得分:184)

使用此:

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;

答案 2 :(得分:18)

合并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子句中引用的列无法更新。

答案 3 :(得分:13)

 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

答案 4 :(得分:7)

如所示here,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
                         )

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

答案 5 :(得分:7)

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

有人建议使用嵌套的SELECT,不要这样做,这是非常缓慢的。如果要更新大量记录,请使用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

另外,请确保您要加入的所有表都有主键。

答案 6 :(得分:2)

以下语法适用于我。

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;

答案 7 :(得分:2)

它的工作正常oracle

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

答案 8 :(得分:1)

使用 description 而不是desc for table2,

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

答案 9 :(得分:0)

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

答案 10 :(得分:0)

ReflectionException                                         
Class ExampleVendor\ExamplePackage\DatabaseStuff\DatabaseSeeder does not exist

答案 11 :(得分:0)

仅出于完整性考虑,并且由于我们在谈论Oracle,因此也可以做到这一点:

declare
begin
  for sel in (
    select table2.code, table2.desc
    from table1
    join table2 on table1.value = table2.desc
    where table1.updatetype = 'blah'
  ) loop
    update table1 
    set table1.value = sel.code
    where table1.updatetype = 'blah' and table1.value = sel.desc;    
  end loop;
end;
/

答案 12 :(得分:0)

Oracle在此方面有不错的表现。

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

通过此链接-我对上面的查询进行了修改,但该修改对我不起作用(来自使用rowid的mathguy的答案)

func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
    return true
}

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

答案 13 :(得分:-1)

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是别名字段,您不需要指向该表。

答案 14 :(得分:-3)

update table1  a 
   set a.col1='Y' 
 where exists(select 1 
                from table2 b
               where a.col1=b.col1 
                 and a.col2=b.col2
             )