我有A表和B表,A表有所有人员的工号的职位信息。
B表有我要更新的部分人员的工号,职位信息。我现在要更新A表,参考数据是是B表中人员的职位信息。如A表有  100 ,null
         200 ,科长
         300,null
         400,经理B表有,100,主管
       300,职员我下面用B表更新A表,这个语句怎么写?

解决方案 »

  1.   


    update  a
    set a.职位信息=(select b.职位信息 from b where a.工号=b.工号)
    where exists (select 1 from from b where a.工号=b.工号 )
      

  2.   

    update a set grade = (select grade from b where a.id = b.id) where exists (select 1 from  b where a.id = b.id)
      

  3.   

    update a set 职位信息=(select 职位信息 from b where b.工号=a.工号)
    where exist (select 职位信息 from b where b.工号=a.工号)
      

  4.   

    看这个帖子,竟然学会了。EXIST的用法。
      

  5.   

    update a set 职位信息=(select 职位信息 from b where b.工号=a.工号)这个就可以了
      

  6.   

    merge into a
    using b
    on(a.no = b.no)
    when matched then
    update a set a.dept=b.dept where a.no=b.no;
      

  7.   


    --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.工号)
      

  8.   

    大家加where exists(select 1 from B where A.工号=B.工号)
    这个条件来爪子啊?我觉得不需要吧
      

  9.   

    update  a
    set a.职位信息=(select b.职位信息 from b where a.工号=b.工号)可以用数据测试一下。
      

  10.   


    where exists (select 1 from from b where a.工号=b.工号 )
      

  11.   

    好像最近好多人在问这个问题啊
    这种用一个表更新另一个表最方便的应该是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;
      

  12.   

    update a
    set a.职位信息=(select b.职位信息 from b where a.工号=b.工号) where a.职位信息 is null
      

  13.   

    update  a
    set a.职位信息=(select b.职位信息 from b where a.工号=b.工号)
    where exists (select 1 from from b where a.工号=b.工号 )
      

  14.   

    不明白为什么还要加个where,是不是画蛇添足
      

  15.   

    update  a
    set a.职位信息=(select b.职位信息 from b where a.工号=b.工号)
    where exists (select 1 from from b where a.工号=b.工号 )
      

  16.   


    这样的话,如果不存在于B表中的那么 职位信息就会被更新为 null 
      

  17.   

    增加exists是为了要确定更新的范围