delete from c
insert into c 
 select a,b,c,d from b
 union all
 select a,b,c,1 from a要实现以上功能 写成一条sql 怎么写才好?

解决方案 »

  1.   

    exec('delete from c;
    insert into c 
     select a,b,c,d from b
     union all
     select a,b,c,1 from a'
    )
      

  2.   

    exec('delete from c; 
    insert into c 
    select a,b,c,d from b 
    union all 
    select a,b,c,1 from a' 
    )
      

  3.   

    CREATE PROC P
    AS 
    BEGIN
    delete from c
    insert into c 
     select a,b,c,d from b
     union all
     select a,b,c,1 from a
    END
      

  4.   

    现学现卖-- SQL Server 2008 statement
    merge c as t
    using (select a,b,c,d from b union all select a,b,c,1 from a) as s(a,b,c,d)
    on (t.a=s.a and t.b=s.b and t.c=s.c and t.d=s.d)
    when not matched by source then delete
    when not matched then insert (a,b,c,d) values(s.a,s.b,s.c,s.d);
      

  5.   

    参考:
    http://school.itzcn.com/index.html#
    上面讲解的比较详细,希望对楼主有所帮助。
      

  6.   

    exec('delete from c; 
    insert into c 
    select a,b,c,d from b 
    union all 
    select a,b,c,1 from a' 
    )
      

  7.   

    exec('delete from c; 
    insert into c 
    select a,b,c,d from b 
    union all 
    select a,b,c,1 from a' 
    )
      

  8.   

    SQL2008有merge,之前的只能分开写,可以放入一个事务中去执行