请教一条更新语句
表结构如下 sp
bm        ck    sl
001      01    1
002      01    2
003      01    3
001      02    4
002      02    5
003      02    6
001      03    7
002      03    8
003      00    9想更新的结果如下 吧02(ck)仓库  下面商品 001,002,003 数量更新到 01仓库上去 商品编码要对应
结果是 
bm        ck    sl
001      01    4
002      01    5
003      01    6
001      02    4
002      02    5
003      02    6
001      03    7
002      03    8
003      03    9
那么一条更新语句可以实现吗,不用临时表处理

解决方案 »

  1.   

    update
      a
    set
      a.sl=b.sl
    from
      sp a
    left join
      sp b
    on
      a.bm=b.bm and a.ck='01' and b.ck='02'
    楼主,把以前的帖子结了吧
      

  2.   

    修正一下:---测试数据---
    if object_id('[tb]') is not null drop table [tb]
    go
    create table [tb]([bm] varchar(3),[ck] varchar(2),[sl] int)
    insert [tb]
    select '001','01',1 union all
    select '002','01',2 union all
    select '003','01',3 union all
    select '001','02',4 union all
    select '002','02',5 union all
    select '003','02',6 union all
    select '001','03',7 union all
    select '002','03',8 union all
    select '003','00',9
     
    ---更新---
    update
      a
    set
      a.sl=b.sl
    from
      tb a
    inner join
      tb b
    on
      a.bm=b.bm and a.ck='01' and b.ck='02'---查询---
    select * from tb---结果---
    bm   ck   sl          
    ---- ---- ----------- 
    001  01   4
    002  01   5
    003  01   6
    001  02   4
    002  02   5
    003  02   6
    001  03   7
    002  03   8
    003  00   9(所影响的行数为 9 行)