簡體   English   中英

將兩個CTE與UNION組合會導致錯誤

[英]Combining two CTEs with UNION causes an error

出現以下錯誤:

'Msg 156,第15級,第1行,第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

在SQL Server中,CTE附加到最外面的select 換句話說,每個查詢只有一個,所有定義都必須在select之前。

因此,將它們with合並為一個:

with cte1 as (
      select . . . 
     ),
     cte2 as (
      select . . .
     )
select . . .
from cte1 . . . 
union
select  . . 
from cte2 . . .;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM