如何使用临时表将2个查询结果并在一起

如何使用临时表将2个查询结果并在一起,在union不好用的时候。这是网上转的,原地址:http://www.cnblogs.com/Wendy_Yu/archive/2011/06/27/2089219.html
--创建用于存储相同数据的临时表
CREATE TABLE #TempTB (
TId int,
TName varchar(50),
SubjectId int,
TType    int
)
--插入考试表数据,1表示是考试
insert into #TempTB(TId,TName,SubjectId,TType)
select EId,EName,SubjectId,EStartTime ,1
from ExamTB
--插入任务表数据,2表示此数据是来自任务表
insert into #TempTB(TId,TName,SubjectId,TType)
select TId,TName,SubjectId,TStartTime,2
from TaskTB
--输出数据
select * from #TempTB
--删除临时表
drop table #TempTB



我后面查到并使用的一种写法

                  select b.StreetletNO,max(b.deepindex) as maxindex  into #T1
                  from T_ASRS_Streetlet_Salver as b  group by b.StreetletNO
                  
                  select b.StreetletNO,min(b.deepindex) as minindex   into #T2
                  from T_ASRS_Streetlet_Salver as b  group by b.StreetletNO
                  
                  select a.StreetletNO,a.maxindex,b.minindex into #T3 from #T1 a left join #T2 b on a.StreetletNO=b.StreetletNO
                  
                  select wp.WHStreetlet,(wp.whdeep-c.maxindex)as lv3, (c.maxindex-c.minindex+1 ) as lv2,(c.minindex-1) as lv1 
                  from T_Warehouse_Position wp left join #T3 c on wp.WHStreetlet = c.StreetletNO
                  where wp.WHArea=@Area
                  
                  drop table #T1
                  drop table #T2
                  drop table #T3

你可能感兴趣的:(SqlServer,存储,数据,sql)