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