表A
列1,列2
A,0
B,0
C,0表B
列1,列2,列3
A,1,qqq
A,1,www
B,1,fff
B,1,sss更新后表A为
列1,列2
A,1
B,1
C,0
表A的其他列不变

解决方案 »

  1.   

    --> 测试数据: #T1
    if object_id('tempdb.dbo.#T1') is not null drop table #T1
    create table #T1 (列1 varchar(1),列2 int)
    insert into #T1
    select 'A',0 union all
    select 'B',0 union all
    select 'C',0
    --> 测试数据: #T2
    if object_id('tempdb.dbo.#T2') is not null drop table #T2
    create table #T2 (列1 varchar(1),列2 int,列3 varchar(3))
    insert into #T2
    select 'A',1,'qqq' union all
    select 'A',1,'www' union all
    select 'B',1,'fff' union all
    select 'B',1,'sss'update #T1
      set 列2=b.列2
     from #T2 b where #T1.列1=b.列1
     
     
    select * from #T1/*
    列1   列2
    ---- -----------
    A    1
    B    1
    C    0(3 行受影响)*/
      

  2.   


    update a set a.c2=b.c2
    from ta a,tb b
    where a.c1=b.c1