我有A表和B表,A表有所有人员的工号的职位信息。
B表有我要更新的部分人员的工号,职位信息。我现在要更新A表,参考数据是是B表中人员的职位信息。如A表有 100 ,null
200 ,科长
300,null
400,经理B表有,100,主管
300,职员我下面用B表更新A表,这个语句怎么写?
B表有我要更新的部分人员的工号,职位信息。我现在要更新A表,参考数据是是B表中人员的职位信息。如A表有 100 ,null
200 ,科长
300,null
400,经理B表有,100,主管
300,职员我下面用B表更新A表,这个语句怎么写?
update a
set a.职位信息=(select b.职位信息 from b where a.工号=b.工号)
where exists (select 1 from from b where a.工号=b.工号 )
where exist (select 职位信息 from b where b.工号=a.工号)
using b
on(a.no = b.no)
when matched then
update a set a.dept=b.dept where a.no=b.no;
--10g的
merge into A using B on(A.工号=B.工号)
when matched then
update set A.职位=B.职位
--版本不行
update A set A.职位=(select 职位 from B where A.工号=B.工号)
where exists(select 1 from B where A.工号=B.工号)
这个条件来爪子啊?我觉得不需要吧
set a.职位信息=(select b.职位信息 from b where a.工号=b.工号)可以用数据测试一下。
where exists (select 1 from from b where a.工号=b.工号 )
这种用一个表更新另一个表最方便的应该是merge,也可以用更新视图的方法
不过oracle会进行键值唯一性检查,如果确定关联条件取值的唯一性可以用提示bypass_ujvc绕过检查update
(
select/*+bypass_ujvc*/ a.value as val_a,b.value as val_b
from a,b
where a.id = b.id
)tmp
set tmp.val_b = tmp.val_a;
set a.职位信息=(select b.职位信息 from b where a.工号=b.工号) where a.职位信息 is null
set a.职位信息=(select b.职位信息 from b where a.工号=b.工号)
where exists (select 1 from from b where a.工号=b.工号 )
set a.职位信息=(select b.职位信息 from b where a.工号=b.工号)
where exists (select 1 from from b where a.工号=b.工号 )
这样的话,如果不存在于B表中的那么 职位信息就会被更新为 null