我有二个结构相同的表A,B, 它们有字段ID,data1,data2 ,现在我想把A表的data1赋值到B表的data1中,根据二者的ID如果相同的话,
为什么这样写不行哦update b   
set b.data1 = (select data1 from a where a.id = b.id)
where exists(select 1 from A where a.id = b.id)为什么这样不行呢,请高手指点下,在线等待,谢谢了!其中ID是NCHAR类型

解决方案 »

  1.   

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

  2.   

    报什么错 是id有n对1的关系还是
    where trim(a.id) = trim(b.id) and rownum-1
      

  3.   

    update b   
    set b.data1 = (select data1 from a where a.id = b.id)
    where exists(select 1 from A where a.id = b.id)
    这个方法好像是可以的,可能是我当时写错了,二楼 加了一个from b 反而报错,提示命令未能正确结束那请问大家,到底是否要加 from b ?
      

  4.   

    还有这句中
    update b   
    set b.data1 = (select data1 from a where a.id = b.id)
    where exists(select 1 from A where a.id = b.id)最后一句 where exists(select 1 from A where a.id = b.id) 有什么作用呢?去掉行吗?
      

  5.   

    2楼from b是多余的最后一句 where exists(select 1 from A where a.id = b.id) 作用是只更新A表中存在B表ID的记录,防止将A表中没有B表对应的ID记录被更新为NULL。也就是说,如果B表中有ID不在A表中,那么B表中此记录是不能更新的,不加这句就会导致B表中该记录的data1被更新为NULL。
      

  6.   

    merge into b
    using a
    on(a.id=b.id)
    when matched then update a.data1 =b.data1 ;
      

  7.   

    谢谢大家的热情的帮助,问题差不多解决了~
    如果我加个条件 A.data1>0 的话,那是不是应该这样写
    update b 
    set b.data1 = (select data1 from a where a.id = b.id)
    where exists(select 1 from A where a.id = b.id and A.data1>0)还有 A,B表ID字段是NCHAR类型 ,写成a.id = b.id 是没问题的吧~
      

  8.   

    2楼的大哥是惯用mssql的了  from b那是mssql的写法