如何使用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
之后,它被编程为工作结束了。如何强制它移动到代码的更多行。
答案 0 :(得分:4)
怎么样:
with CTE1 as select (...)
insert into table1 select <cols> from CTE1
GO
with CTE2 as select (...)
insert into table2 select <cols> from CTE2
GO
答案 1 :(得分:2)
你不能用CTE做到这一点。
CTE仅在查询范围内可用,您不能使用一个查询来执行两个insert
语句。
在你的情况下,我只会这样做:
insert into table1 select ... /* the select you had in CTE1*/
insert into table2 select ... /* the select you had in CTE2*/