有两个表t1                                           t2
        id1 n1      n2                               id2  id1  n1  n2 
         1  张三    1988-10-8                         1    1  
         2  李四    1985-6-8                          2    2
         。                                。。
t2.id1是t1.id1的外建 现在要按照管理将t1中n1,n2的数值添加到t2的n1,n2中 请问该怎么写?(t2.id2不一定是连续的)

解决方案 »

  1.   

    update t2 a
    set n1 = b.n1,n2 = b.n2
    from t1 b
    where b.id1 = a.id1
      

  2.   

    update a
    set n1 = b.n1,n2 = b.n2
    from t1 b,t2 a 
    where b.id1 = a.id1
      

  3.   

                             
           declare @t1 table( id1 int , n1 Nvarchar(5) ,   n2 datetime)
           insert @t1 select  1 , N'张三' ,   '1988-10-8'             
           insert @t1 select   2,  N'李四' ,   '1985-6-8'             
           declare @t2 table(id2 int , id1 int ,  n1 Nvarchar(5) , n2 datetime )
         insert @t2  select 1,1,null,null
        insert @t2 select 2,2,null,null
    update a
    set n1 = b.n1,n2 = b.n2
    from @t1 b,@t2 a 
    where b.id1 = a.id1
    select * from @t2
    /*
    id2         id1         n1    n2                                                     
    ----------- ----------- ----- ------------------------------------------------------ 
    1           1           张三    1988-10-08 00:00:00.000
    2           2           李四    1985-06-08 00:00:00.000(影響 2 個資料列)*/
      

  4.   


    update t2 set n1 = b.n1,n2 = b.n2
    from t2 a,t1 b 
    where b.id1 = a.id1
      

  5.   

    update t2 
    set t2.n1 = t1.n1,t2.n2 = t1.n2
    from t1 ,t2
    where t1.id1 = t2.id1
      

  6.   

    update a
    set n1 = b.n1,n2 = b.n2
    from @t1 b,@t2 a 
    where b.id1 = a.id1
    select * from @t2
      

  7.   

    update b
    set id1= a.id1,n1=a.n1,n2=a.n2
    from t1 a,t2 b
    where a.id1=(select count(*) from t2 where id2<=b.id2)
      

  8.   


    update a
    set n1 = b.n1,n2 = b.n2
    from t1 b,t2 a 
    where b.id1 = a.id1