具有内部联接的Oracle Update语句

时间:2011-10-05 16:06:58

标签: oracle inner-join

我正在尝试使用内部联接编写一个简单的更新语句,但我在SQL Server中执行此操作的方式似乎在ORACLE中不起作用。这是更新:

UPDATE D
SET D.USER_ID = C.USER_ID
FROM D INNER JOIN C 
ON D.MGR_CD = C.MGR_CD WHERE D.USER_ID IS NULL;

似乎我得到的错误是FROM。有人可以解释一下,解决这个问题的原因以及如何解决这个问题?

1 个答案:

答案 0 :(得分:7)

在Oracle中,您不能以update statement方式使用from子句。以下任何一项都应该有效。

UPDATE d
SET    d.user_id   =
          (SELECT c.user_id
           FROM   c
           WHERE  d.mgr_cd = c.mgr_cd)
WHERE  d.user_id IS NULL;

UPDATE (SELECT d.user_id AS d_user_id, c.user_id AS c_user_id
        FROM   d INNER JOIN c ON d.mgr_cd = c.mgr_cd
        WHERE  d.user_id IS NULL)
SET    d_user_id   = c_user_id;

UPDATE (SELECT d.user_id AS d_user_id, c.user_id AS c_user_id
        FROM   d INNER JOIN c ON d.mgr_cd = c.mgr_cd)
SET    d_user_id   = c_user_id
WHERE  d_user_id IS NULL;

但是,我倾向于在此方案中使用MERGE

MERGE INTO d
USING      c
ON         (d.mgr_cd = c.mgr_cd)
WHEN MATCHED THEN
    UPDATE SET d.user_id = c.user_id
        WHERE      d.user_id IS NULL;