table a
字段:
Id,Code,des
唯一键 Id,Code
数据:
1 , 'CN' , '水'
2 , 'CN' , '火'
table b
字段:
Id,Code,des
唯一键 Id,Code
数据:
1 , 'CN' , 'shui'
2 , 'CN' , 'huo'问题:怎样通过update,从a表更新b表的数据.
得到这样的结果:
1 , 'CN' , '水'
2 , 'CN' , '火'
我写的如下:
update b set b.des=
(select a.des 
from a 
where a.id=b.id 
and a.Code = b.Code 
and a.Code = 'CN')
这样写的b表结果是:
1 , 'CN' , '水'
2 , 'CN' , '水'
如果我写
update b set (b.id,b.des)=
(select a.id,a.des 
from a 
where a.id=b.id 
and a.Code = b.Code 
and a.Code = 'CN')
则提示关于违反唯一约束性.

解决方案 »

  1.   

    看来要明天继续等高人啦,HELP啊
      

  2.   

    update b set b.des=
    (select a.des 
    from a 
    where a.id=b.id 
    and a.Code = b.Code 
    and a.Code = 'CN')
    where exists (select * from a where a.id=b.id 
       and a.Code = b.Code 
       and a.Code = 'CN')
      

  3.   

    where exists (select * from a where a.id=b.id 
       and a.Code = b.Code 
       and a.Code = 'CN')
    这个我已经加的了,多谢你的提供.
    只是我想要的结果依然不对
      

  4.   

    是不是要用一个游标打开后,一条一条的做UPDATE?
      

  5.   

    明白了,我这里写的对的.但是做的时候写错了.多谢
    update b set b.des=
    (select a.des 
    from a 
    where a.id=b.id 
    and a.Code = b.Code 
    and a.Code = 'CN')
    where exists (select 1 from a where a.id=b.id 
       and a.Code = b.Code 
       and a.Code = 'CN')
    也是正确的
      

  6.   

    update tableb b set b.des=(select a.des from tablea a where a.id=b.id and a.code=b.code)