tsql – t-sql WITH WITH WITH

前端之家收集整理的这篇文章主要介绍了tsql – t-sql WITH WITH WITH前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我必须对WITH查询进行查询,就像
; WITH #table1  
(  
SELECT id,x from ... WHERE....  
UNION ALL  
 SELECT id,x from ... WHERE...  
)  

WITH #table2  
(  
 SELECT DISTINCT tbl_x.*,ROW_NUMBER() OVER (order by id) as RowNumber  
WHERE id in ( SELECT id from #table1)  
)  

SELECT * FROM #table2 WHERE RowNumber > ... and ...

所以我必须使用WITH WITH WITH然后SELECT在第二个WITH,我该怎么做?

解决方法

您可以在WITH关键字之后定义多个CTE,通过用逗号分隔每个CTE.
WITH T1 AS
(
    SELECT id,x from ... WHERE....
    UNION ALL
    SELECT id,x from ... WHERE...
),T2 AS
(
    SELECT DISTINCT tbl_x.*,ROW_NUMBER() OVER (order by id) as RowNumber
    WHERE id in ( SELECT id from T1 )
)
SELECT * FROM T2 WHERE RowNumber > ... and ...

http://www.4guysfromrolla.com/webtech/071906-1.shtml

猜你在找的MsSQL相关文章