相关疑难解决方法(0)

如何在单个SELECT语句中使用多个公用表表达式?

我正在简化复杂的select语句,所以我想我会使用常用的表表达式.

声明一个cte工作正常.

WITH cte1 AS (
    SELECT * from cdr.Location
    )

select * from cte1 
Run Code Online (Sandbox Code Playgroud)

是否可以在同一个SELECT中声明和使用多个cte?

即这个sql给出了一个错误

WITH cte1 as (
    SELECT * from cdr.Location
)

WITH cte2 as (
    SELECT * from cdr.Location
)

select * from cte1    
union     
select * from cte2
Run Code Online (Sandbox Code Playgroud)

错误是

Msg 156, Level 15, State 1, Line 7
Incorrect syntax near the keyword 'WITH'.
Msg 319, Level 15, State 1, Line 7
Incorrect syntax near the keyword 'with'. If this statement is a common table …
Run Code Online (Sandbox Code Playgroud)

sql t-sql sql-server common-table-expression sql-server-2008

90
推荐指数
2
解决办法
6万
查看次数