0

收到以下错误:

'Msg 156, Level 15, State 1, Line 53
关键字'WITH'附近的语法不正确。

消息 319,级别 15,状态 1,第 53 行
关键字“with”附近的语法不正确。如果此语句是公用表表达式、xmlnamespaces 子句或更改跟踪上下文子句,则前面的语句必须以分号结束。

单独运行 CTE 时没问题。但是当使用 UNION.. 组合时,就会出现错误。

With cte1 as 
(
    select
       .
       .
       .

    Select

    from

)
select

UNION

With cte2 as 
(
    Select

    From
)
Select 

from
4

1 回答 1

5

在 SQL Server 中,CTE 附加到最外层的select. 换句话说,每个查询只有一个,所有定义都必须在select.

所以将它们组合成一个with

with cte1 as (
      select . . . 
     ),
     cte2 as (
      select . . .
     )
select . . .
from cte1 . . . 
union
select  . . 
from cte2 . . .;
于 2018-08-19T11:33:00.793 回答