update test1
set 成绩=test2.成绩
from test2
where test1.年=test2.年 and test1.季度=test2.季度 and test1.学号=test2.学号insert test1
select * from test2 t
where not exists(select 1 from test1 where 年=t.年 and 季度=t.季度 and 学号=t.学号)

解决方案 »

  1.   

    --更新已经存在的记录
    update a set 成绩=b.成绩 from test1 a,test2 b where a.年=b.年 and a.季度=b.季度 and a.学号=b.学号--insert不能存在的记录
    insert into test1 select * from test2 t where not exists(select 1 from test1 where 年=t.年 and 季度=t.季度 and 学号=t.学号)
      

  2.   

    declare @t1 table(年 int,季度 int,学号 int,成绩 int)
    insert into @t1 select 30,1,100000,12 
    insert into @t1 select 30,1,100001,14 
    insert into @t1 select 29,2,100000,14 
    declare @t2 table(年 int,季度 int,学号 int,成绩 int)
    insert into @t2 select 30,2,100000,15 
    insert into @t2 select 30,2,100003,16 --更新已经存在的记录
    update a set 成绩=b.成绩 from @t1 a,@t2 b where a.年=b.年 and a.季度=b.季度 and a.学号=b.学号--insert不存在的记录
    insert into @t1 select * from @t2 t where not exists(select 1 from @t1 where 年=t.年 and 季度=t.季度 and 学号=t.学号)--查看结果
    select * from @t1
    /*
    年           季度          学号          成绩          
    ----------- ----------- ----------- ----------- 
    30          1           100000      12
    30          1           100001      14
    29          2           100000      14
    30          2           100000      15
    30          2           100003      16
    */
      

  3.   

    insert test1
    select * from test2 a
    where not exists
    (
    select 1 
    from test1 
    where 年=a.年 
    and 季度=a.季度 
    and 学号=t.学号
    )update test1
    set 成绩=a.成绩
    from test2 a
    where test1.年=a.年 
    and test1.季度=a.季度 
    and test1.学号=a.学号
      

  4.   

    個人覺得要分開寫更新與插入
     
    update test1
    set test1.成績=test2.成績
    where test1.年=test2.年
          and test1.季度=test2.季度
          and test1.學號=test2.學號
    insert into test1 (年,季度,學號,成績)
    values (select 年,季度,學號,成績 from test2
             where test1.年<>test2.年
                     or test1.季度<>test2.季度
                     or test1.學號<>test2.學號)
      

  5.   

    update test1
    set 成绩=test2.成绩
    from test2
    inner join
    test1 as t1 
    on t1.年=t2.年 and t1.季度=t2.季度 and t1.学号=t2.学号insert test1(年,季度,学号,成绩)
    select t2.年,t2.季度,t2.学号,t2.成绩 
    from test2 as t2
    left outer join
    test1 as t1 
    on t1.年=t2.年 and t1.季度=t2.季度 and t1.学号=t2.学号
    where t1.年 is null and t1.季度 is null and t1.学号 is null
      

  6.   

    insert test1
    select * from test2 a
    where not exists
    (
    select 1 
    from test1 
    where 年=a.年 
    and 季度=a.季度 
    and 学号=a.学号
    )update test1
    set 成绩=a.成绩
    from test2 a
    where test1.年=a.年 
    and test1.季度=a.季度 
    and test1.学号=a.学号
      

  7.   

    select * from test2 
    union all
    select * from test1
     where 年 in(select t2.年 from test1 t1,test2  t2 
    where t1.年=t2.年 and t1.季度=t2.季度 and t1.学号=t2.学号) and
    季度 in(select t2.季度 from test1 t1,test2  t2 
    where t1.年=t2.年 and t1.季度=t2.季度 and t1.学号=t2.学号) and
    学号 in(select t2.学号 from test1 t1,test2  t2 
    where t1.年=t2.年 and t1.季度=t2.季度 and t1.学号=t2.学号)
      

  8.   

    delete from test1 a where exists (select 1 from test2 b
        where b.[年]=a.[年] and b.[季度]=a.[季度] and b.[学号]=a.[学号])insert into test1 select * from test2