gpt4 book ai didi

sql-server - 如何将动态 sql 插入临时表?

转载 作者:行者123 更新时间:2023-12-03 23:54:22 25 4
gpt4 key购买 nike

我有这个动态查询,如何将它的结果插入到临时表中?
此查询的结果显示 (1000 row(s) affected)但是有机会在临时表中转储这 1000 行吗?

类似的东西:

INSERT INTO #TempTable
EXEC(@query)

这是我的查询
DECLARE @cols AS NVARCHAR(MAX),
@query AS NVARCHAR(MAX)

SET @cols = STUFF((SELECT ',' + QUOTENAME(c.locationCode)
FROM Catalytic_vw_LocationCodeByLine c WHERE c.linename ='wind' order by c.CompanyName, c.LocationCode
FOR XML PATH('')),1,1,'')

set @query =
'select * into ##Temp
from

(SELECT QUOTEGUID as qguid, ' + @cols + ' from
(
select
QuoteGUID,
LocationCode,
LineName,
LineGUID
from Catalytic_vw_PolicyLocationCode
) x
pivot
(
max(locationCode)
for locationCode in (' + @cols + ')
)p)x'

EXEC sp_executesql @query;

最佳答案

我运行这段代码,它返回了我创建的测试行。

declare @query nvarchar(100)
set @query = N'select * into ##TMPTblTest from tblTest'

exec sp_executesql @query;

select * from ##TMPTblTest

您正在使用全局临时表。如果您对其进行选择,我认为它会起作用。

关于sql-server - 如何将动态 sql 插入临时表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43527375/

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