0

我有以下结构:

Create @temp
Select ...inser...into @temp where ...

(select ... from @temp
Join tblA where ... )
UNION
(Select ... from @temp
join tblB where ... )

在建立上表之后,我需要能够执行 WHERE、JOINS、...

就像是:

Select ... from (above statement)
join ....
where....

我不知道 @temp,joins, union... 是否可以在其他选择中。

或者我唯一能做的就是创建一个带有第一个语句结果的@Temp2 插入,然后与其他连接一起工作,在哪里......?

更新 1:

我也在尝试:

With cte (query returned columns)
as
(same query I was using to build my @temp as before)

(select ... from cte
join tblA
where...)
UNION
(select ... from cte
join tblB
where...)

但是我在如何执行其他连接方面停留在同一点上,其中......总结果高于

4

2 回答 2

2
Create @temp
Select ...inser...into @temp where ...

;with temp2 as
(
    select ... from @temp Join tblA where ...
    UNION
    Select ... from @temp join tblB where ... 
)
select ... from temp2
join ....
where....
于 2015-05-15T12:29:11.313 回答
1

实际上你可以在没有临时表的情况下做到这一点:

WITH myCTE [ ( column_name [,...n] ) ]
AS
( here you define your query )

然后你只做你的选择但使用CTE

Select ... from myCTE
join ....
where....

关于 CTE,您可以在更新后阅读此处

Select fields from myCTE join table1
Union
Select fields from myCTE join table2

查询中没有括号

于 2015-05-15T12:32:31.150 回答