SqlServer|如何使用临时表将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, TTypeint ) --插入考试表数据,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 maxindexinto #T1 from T_ASRS_Streetlet_Salver as bgroup by b.StreetletNOselect b.StreetletNO,min(b.deepindex) as minindexinto #T2 from T_ASRS_Streetlet_Salver as bgroup by b.StreetletNOselect a.StreetletNO,a.maxindex,b.minindex into #T3 from #T1 a left join #T2 b on a.StreetletNO=b.StreetletNOselect 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=@Areadrop table #T1 drop table #T2 drop table #T3


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

    推荐阅读