现有表a
编号,备注1,备注2
2115 150 null
2116 151 null
2117 152 null
表b
编号,备注2
2115 250
2116 251
2117 252
现在需要将表b里面备注2的内容插入到表a的备注2里,也就是最后需要出现
表a(编号,备注1,备注2),
2115 150 250
2116 151 251
2117 152 252
的样式,请问各位大大 更新的语句改怎么写?
……谢谢各位……

解决方案 »

  1.   

    update 表a set 备注2 = b.备注 from 表a as a join 表b as b on a.编号 = b.编号
      

  2.   

    --> 测试数据: #a
    if object_id('tempdb.dbo.#a') is not null drop table #a
    create table #a (编号 int,备注1 int,备注2 varchar(50))
    insert into #a
    select 2115,150,null union all
    select 2116,151,null union all
    select 2117,152,null
    --> 测试数据: #b
    if object_id('tempdb.dbo.#b') is not null drop table #b
    create table #b (编号 int,备注2 int)
    insert into #b
    select 2115,250 union all
    select 2116,251 union all
    select 2117,252update #a set 备注2 = b.备注2 from #a as a join #b as b on a.编号 = b.编号select * from #a
      

  3.   

    现有表a 
    编号,备注1,备注2 
    2115 150 null 
    2116 151 null 
    2117 152 null 
    表b 
    编号,备注2 
    2115 250 
    2116 251 
    2117 252 
    现在需要将表b里面备注2的内容插入到表a的备注2里,也就是最后需要出现 
    表a(编号,备注1,备注2), 
    2115 150 250 
    2116 151 251 
    2117 152 252 
    的样式,请问各位大大 更新的语句改怎么写? 
    ……谢谢各位……update A set 备注2 = B.备注2 from A,B where A.编号 = B.编号
      

  4.   

    create table a(编号 int,备注1 int,备注2 varchar(10))
    insert into a values(2115,150,null)
    insert into a values(2116,151,null)
    insert into a values(2117,152,null)
    create table b (编号 int,备注2 int)
    insert into b values(2115,250)
    insert into b values(2116,251)
    insert into b values(2117,252)
    goupdate A set 备注2 = B.备注2 from A,B where A.编号 = B.编号select * from Adrop table A,B/*
    编号          备注1         备注2        
    ----------- ----------- ---------- 
    2115        150         250
    2116        151         251
    2117        152         252
    (所影响的行数为 3 行)
    */