数据库:Oracle
我想基于以下条件将列ClientCode值“30”更新为“40”:
如果a.Newcode(12.1.1)= b.Old_code(12.1.1) 并且b.new_code为空
我已尝试过以下查询但到目前为止没有成功;
UPDATE CLIENTACTIVITYCODEMAPPING a
SET a.CLIENTCODE = (SELECT B.CLIENTCODE FROM CLIENTACTIVITYCODEMAPPING b
WHERE b.NEW_CODE IS NULL
AND b.PROJECTCODE = A.PROJECTCODE
AND b.OLD_CODE = a.NEW_CODE)
WHERE a.PROJECTCODE = 2242
AND a.NEW_CODE IS NOT NULL
AND A.NEW_CODE IN (SELECT c.OLD_CODE FROM CLIENTACTIVITYCODEMAPPING c where c.new_code is null and c.old_code = a.new_code)
答案 0 :(得分:0)
一种解决方案是将相关子查询添加到where
子句中:
UPDATE CLIENTACTIVITYCODEMAPPING a
SET a.CLIENTCODE = (SELECT B.CLIENTCODE
FROM CLIENTACTIVITYCODEMAPPING b
WHERE b.NEW_CODE IS NULL AND b.PROJECTCODE = A.PROJECTCODE AND
b.OLD_CODE = a.NEW_CODE
)
WHERE a.PROJECTCODE = 2242 AND a.NEW_CODE IS NOT NULL and
A.NEW_CODE IN (SELECT c.OLD_CODE FROM CLIENTACTIVITYCODEMAPPING c where c.new_code is null and c.old_code = a.new_code) and
exists (SELECT B.CLIENTCODE
FROM CLIENTACTIVITYCODEMAPPING b
WHERE b.NEW_CODE IS NULL AND b.PROJECTCODE = A.PROJECTCODE AND
b.OLD_CODE = a.NEW_CODE
)
另一种方法是使用coalesce()
在新NULL
时保持相同的值:
UPDATE CLIENTACTIVITYCODEMAPPING a
SET a.CLIENTCODE = coalesce((SELECT B.CLIENTCODE
FROM CLIENTACTIVITYCODEMAPPING b
WHERE b.NEW_CODE IS NULL AND b.PROJECTCODE = A.PROJECTCODE AND
b.OLD_CODE = a.NEW_CODE
), a.clientcode)
WHERE a.PROJECTCODE = 2242 AND a.NEW_CODE IS NOT NULL and
A.NEW_CODE IN (SELECT c.OLD_CODE FROM CLIENTACTIVITYCODEMAPPING c where c.new_code is null and c.old_code = a.new_code)