承认自己是有点懒了……表1:字段A,字段B
表2:字段C,字段D(需要的)临时表:字段E,字段Finsert into #临时表
select * from 表1
union
select* from 表2表1的字段A+表2的字段C=临时表的字段E
表1的字段B+表2的字段D=临时表的字段F1、不知道怎么对应起来……
2、写在存储过程里面时,是否需要声明创建#临时表?或者是直接写?

解决方案 »

  1.   

    不需要临时可以解决,直接写
    insert into 表名(E,F)
    select  sum(A) as A,sum(B) as b
    from(
    select A,B from 表1 
    union select C,D from 表2 
    )t
      

  2.   


    --不声明select e,f into #tb
    from (
        select a as e,b as f from tb1
        union all
        select c,d from tb2
    ) tmp--另外需注意对应字段的数据类型是否可以这样做union all。
      

  3.   

    --不声明select e,f into #tb
    from (
        select a as e,b as f from tb1
        union all
        select c as e,d as f from tb2
    ) tmp--另外需注意对应字段的数据类型是否可以这样做union all。应该是这样?