子查询更新Update字段值
2017-10-16 本文已影响0人
CJDUDU
在进行数据修复时,遇到一个问题一直没有怎么注意的问题。
update wf_task wf
set ETime =
(select NewEtime
from wf_task_Mod_backup_20171016 a
where wf.Entgid = a.EntGid
and wf.Flowgid = a.FlowGid
and wf.taskGid = a.taskGid
and wf.execcode = a.execcode
and wf.code = a.code
and wf.btime = a.btime)
以上执行的结果会让匹配的数据更新为目标数据,但是不匹配的都会被更新为null
update wf_task wf
set ETime =
(select NewEtime
from wf_task_Mod_backup_20171016 a
where wf.Entgid = a.EntGid
and wf.Flowgid = a.FlowGid
and wf.taskGid = a.taskGid
and wf.execcode = a.execcode
and wf.code = a.code
and wf.btime = a.btime)
where exists (select 1
from wf_task_Mod_backup_20171016
where Entgid = wf.Entgid
and FlowGid = wf.flowgid
and TaskGid = wf.Taskgid);
所以要在后面加上exists,限制数据更新的范围。切忌update 表 set = (字段)后面没有接where 条件。