在sql server中联合递归查询

时间:2012-05-23 20:59:36

标签: sql sql-server recursive-query

我需要结合两个不同的递归查询。有些想法:

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

union all

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

我该怎么办?我收到错误: 关键字“WITH”附近的语法不正确。

你知道任何workaroud吗?我试图通过互联网搜索,但我找不到它..

1 个答案:

答案 0 :(得分:9)

订单应该是:

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

您的所有CTE都需要在系列语句的开头。