插入表变量 CTE [英] Insert Into Table Variable CTE
本文介绍了插入表变量 CTE的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何将cte的结果插入到表变量中
How to insert the results of a cte into a table variable
类似的东西?
DECLARE
@myData TABLE(
Title nvarchar(350) NOT NULL ,
Id int NOT NULL ,
);
INSERT INTO @myData
with CTE as
(SELECT
a.Title
,a.Id
FROM
TableA
)
,CTE2 as
(SELECT
b.Title
,b.Id
FROM
TableB
)
Select * From CTE
union all
Select * From CTE2
Select ROW_NUMBER() OVER(ORDER BY GetDate() DESC) AS RowId
, x.*
From @myData x
order by x.Id desc
推荐答案
试试这个
with CTE as
(SELECT
a.Title
,a.Id
FROM
TableA
)
,CTE2 as
(SELECT
b.Title
,b.Id
FROM
TableB
)
INSERT INTO @myData --- insert statement goes here after CTE
Select * From CTE
union all
Select * From CTE2
Select ROW_NUMBER() OVER(ORDER BY GetDate() DESC) AS RowId, x.*
From @myData x
order by x.Id desc
这篇关于插入表变量 CTE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文