从表1中select出如下数据(Table1):Select Date, volume from Table1
Date       volume
2007-7-1    100
2007-7-2    80
2007-7-5    20
2007-7-6    5
2007-7-9    25
2007-7-10   35从表2中select出如下数据(Table2):Select Date, volume from Table2
Date       volume
2007-7-1    30
2007-7-3    80
2007-7-5    60
2007-7-6    5
2007-7-8    25从表3中select出如下数据(Table3):Select Date, volume from Table3
Date       volume
2007-7-2    3
2007-7-3    8
2007-7-4    6
2007-7-6    5
2007-7-11   24有很多表Table....类似上面select 出类似的数据。我的问题:
能否建立一个存储过程,建立一个临时表,临时表有3个字段Date,Volume,Type.Type说明来自哪一个表。
最后的临时表中的内容为:Date       volume  Type
2007-7-1    100     T1
2007-7-2    80      T1
2007-7-5    20      T1
2007-7-6    5       T1
2007-7-9    25      T1
2007-7-10   35      T1
2007-7-1    30      T2
2007-7-3    80      T2
2007-7-5    60      T2
2007-7-6    5       T2
2007-7-8    25      T2
2007-7-2    3       T3
2007-7-3    8       T3
2007-7-4    6       T3
2007-7-6    5       T3
2007-7-11   24      T3

解决方案 »

  1.   

    Select Date, volume,'T1' as type into #tmp from Table1
    union all
    Select Date, volume,'T2' as type from Table2
    union all
    Select Date, volume,'T3' as type from Table3----查看
    select * from #tmp
      

  2.   

    select *, 't1' type from t1
    union all
    select *, 't2' type from t2
    union all
    select *, 't3' type from t3
      

  3.   

    create proc sp_ins
    as
    begin
      if object_id('#tmp') is not null
        drop table #tmp  select Date, Volume, 'T1' as Type into #tmp from Table1  insert #tmp
        select Date, Volume, 'T2' from Table2
      union all
        select Date, Volume, 'T3' from Table3
      union all
        select Date, Volume, 'T4' from Table4
    end
      

  4.   


    Select Date, volume,type = 'T1'into #temp from Table1 union all
    Select Date, volume,type = 'T2' from Table2 union all
    Select Date, volume,type = 'T3' from Table3 union all
    select * from #temp