gpt4 book ai didi

sql-server-2008 - 插入表变量CTE

转载 作者:行者123 更新时间:2023-12-04 07:06:06 25 4
gpt4 key购买 nike

如何将cte的结果插入表变量

像这样吗

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

关于sql-server-2008 - 插入表变量CTE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23026240/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com