簡體   English   中英

如何在SQL Server中使用CTE執行多個查詢?

[英]How to execute more than one query with CTE in SQL server?

如何使用CTE執行多個查詢。

with 
CTE1 as select (...)
,
CTE2 as select (...)

insert into table1 from CTE1
insert into table2 from CTE2

我收到一個錯誤Invalid object name CTE2 它看不到我的CTE2 似乎它僅執行一個查詢。 insert into table1 from CTE1已編程完成作業。 如何迫使它移動到代碼的下一行。

關於什么:

with CTE1 as select (...)
insert into table1 select <cols> from CTE1
GO

with CTE2 as select (...)
insert into table2 select <cols> from CTE2
GO

您不能使用CTE做到這一點。

CTE僅在查詢范圍內可用,並且您不能使用一個查詢執行兩個insert語句。

在您的情況下,我將簡單地執行以下操作:

insert into table1 select ... /* the select you had in CTE1*/
insert into table2 select ... /* the select you had in CTE2*/

暫無
暫無

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

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