table1   
id                  name1   name2 
1                   张三     张三 
2                   李四     李四 
3                   王五     王五
(此表ID号唯一,且不为空) table2 
id                  name1      name2
1                   张飞        张飞 
空                     刘备        刘备
2                   项少龙     项少龙
2                   项少龙     项少龙 
(此表ID列值不唯一,也有可能为空) 求助:如何将table2表中name1替换为table1的name1,table1表中name2替换为table2的name2,期望结果如下: 
table1   
id                  name1   name2 
1                   张三     张飞
2                   李四     项少龙 
3                   王五     王五
table2 
id                  name1      name2
1                   张三        张飞 
空                     刘备        刘备
2                   李四        项少龙
2                   李四        项少龙 

解决方案 »

  1.   

    把TABLE2的name2更新到table1,写个过程:
    declare
    cursor aaa is select * from table2;
    begin
    for rec in aaa loop
    update table1 set name2=(select name2 from table2 where id=rec.id and rownum=1)
    where id=rec.id;
    end loop;
    commit;
    end;
    /更新table1到table2也改下就行了。
      

  2.   


    update table2 b 
    set b.name1 = (select a.name1 from table1 a where a.id = b.id)
    where exists(select * from table1 c where c.id = b.id)