有两个表TB1,TB2,TB1里包含字段A1,A2,A3,TB2里包含字段B1,B2,B3。在TB1.A1=TB2.B1的条件下,我想把TB2里的B2内容修改成TB1的A2内容。这样的SQL语句怎么写?

解决方案 »

  1.   

    update TB2
    set B2=A2
    from TB2,TB1
    where TB1.A1=TB2.B1
      

  2.   


    update tb2 set b2 = tb1.a2 from tb2 , tb1 where tb2.b1 = tb1.a1
      

  3.   

    update TB2
    set B2 = (select A2 from TB1 where TB2.B1=A1)
      

  4.   

    此写法,会把不满足TB1.A1=TB2.B1条件的B2更新为null. 错误的!!
    1,2楼的写法都是正确的
      

  5.   


    update b set b.b2=a.a2 from tb1 a join tb2 b on a.a1=b.b1
      

  6.   

    update tb2 
      set b2 = tb1.a2 
    from tb2 , tb1
     where tb2.b1 = tb1.a1
      

  7.   

    update Tb2 set Tb2.b2=Tb1.A2 from tb1,tb2 where Exists (select tb1.A1 from tb1 inner join Tb2 on tb1.A1=tb2.b1 )
      

  8.   

    update tb2 set b2 = tb1.a2 from tb2,tb1 where tb2.b1 = tb1.a1
      

  9.   


    if object_id('test1') is not null
    drop table test1
    if object_id('test2') is not null
    drop table test2go
    create table test1(id int ,id1 int)
    insert into test1 select 1,11 
    create table test2(id int ,id1 int)
    insert into test2 select 2,2
    union select 1,3
    go
    update test2 
    set test2.id1=test1.id1
    from test1,test2
    where test1.id=test2.id
    go
    select * from test2
      

  10.   

    update b set b.b2=a.a2 from tb1 a join tb2 b on a.a1=b.b1