2012-05-23 89 views
1

我需要聯合兩個不同的遞歸查詢。 Somethink like:在sql server中聯合遞歸查詢

with cte1 as 
(select.... 
.... 
.... 
) 
select * from cte1 

union all 

with cte2 as 
(select.... 
.... 
.... 
) 
select * from cte2 

我該怎麼辦?我收到錯誤: 關鍵字'WITH'附近的語法錯誤。

你知道任何workaroud嗎?我試圖尋找過互聯網,但我不能找到它..

回答

9

的順序應該是:

;WITH Cte1 AS 
(
.... 
), 
Cte2 AS 
(
.... 
) 
SELECT * FROM Cte1 
UNION ALL 
SELECT * FROM Cte2 

你的所有CTE的需要是在一系列的語句開頭。

+0

哦...我想我的大腦今天不工作。謝謝 – nosbor