2013-04-24 53 views
0

因此,我提出了一个关于如何摆脱#temp表的问题。如何摆脱SQL Server中的#temp表和CTE

How to get rid of #temp tables

,并得到解决太..但我不希望使用CTE为好。

任何想法,如何将所有这些查询合并为一个。

select {some columns} into #temp1 from {some tables} where {conditions1} 
select {some other columns} into #temp2 from {some tables} where {conditions2} 
select {some other columns} into #temp3 from {some tables} where {conditions3} 

select {some columns from all #temp tables} from #temp1, #temp2, 
#temp3 where {conditions} 

感谢

+2

没有表脚本,所需的列,WHERE和JOIN条件,我们无法真正帮助 – gbn 2013-04-24 09:37:25

+0

@gbn,完全同意......但我无法真正发布我的查询。谢谢 – Jaikrat 2013-04-24 09:51:08

+0

其实,我已经解决了它......但我也得到了一些其他例外。 – Jaikrat 2013-04-24 09:51:37

回答

1

这完全是我不清楚你想达到什么,但如果你不希望使用热膨胀系数(不管是什么奇怪的原因),派生表可以做你想要什么:

select {some columns 
from ( 
    select {some columns} 
    from {some tables} 
    where {conditions1} 
) as t1 
    join ( 
    select {some other columns} 
    from {some tables} 
    where {conditions2} 
) as t2 ON {join condition between t1 and t2} 
    join (
    select {some other columns} 
    from {some tables} 
    where {conditions3} 
) as t3 ON {join condition between t2 and t3} 
where {conditions} 

(虽然在技术上没有真正的改变,这仅仅是写同样的事情用不同的方式)

+0

你是对的......它只是另一种写CTE材料的方式......但我认为,我也不能使用它。我想为同样的单个查询写。 – Jaikrat 2013-04-24 09:53:51