两张表:a和b
     a表中有wlid,weight两个字段
      b表中也有wlid,weight俩个字段
我要修改a表中的weight=b.weight,条件是俩表的wlid相同

解决方案 »

  1.   


    update a
    set a.weight = b.weight
    from a,b
    where a.wlid = b.wlid
      

  2.   

    update a
    set a.weight = b.weight
    from a,b
    where a.wlid = b.wlid
      

  3.   

    报错了.....
    Cannot insert the value NULL into column 'weight', table 'MRP2.dbo.dWLID'; column does not allow nulls. UPDATE fails.
    The statement has been terminated.
      

  4.   

    是不是我的表有问题,weight这列我是不允许为空的
      

  5.   

    update a
    set a.weight = b.weight
    from a,b
    where a.wlid = b.wlid
      

  6.   

    这条语句能实现,但是不符合我现在的情况
    因为b表中的资料并不全等于a表的资料,就是说b的资料少于a的资料
    而且a表中的weight这个字段是不允许为空的
    可不可判断如果为空的就用 0 来代替
      

  7.   

    是你的表 weight字段设置了不能为空
      

  8.   

    --将a中weight为null且不存在于b的设为0
    update a
    set weight = 0
    where not exists(select 1 from b where a.wlid = b.wlid)
    and a.weight  is null--更新存在于b的
    update a
    set a.weight = isnull(b.weight,0)
    from a,b
    where a.wlid = b.wlid
      

  9.   

    是我弄错了,不好意思
    B表是EXCEL导入的,里面有null没注意