从动态查询表输出将数据插入临时表

时间:2016-11-10 06:14:38

标签: sql-server tsql dynamic-sql

我在SQL Server中运行以下动态查询,使用链接服务器连接到OLAP服务器,该服务器返回一个表作为结果。

SET @nSQL = EXECUTE ('SELECT non empty {
[Coded Season].[Coded Season].[Coded Season] *
[Season].[Season].[Season] *
[Product].[Subclass].[Subclass] *
[Product].[Subclass Id].[Subclass Id]
} ON ROWS,{
[Measures].[Pl No of Range Opts]
} ON COLUMNS
FROM RP_C0') AT AS_T_RP_5900_Admin

我在SQL Server中执行它,如下所示:

exec sp_executesql @nSQL;

它返回一个值表。现在我想将数据插入临时表。我已经尝试了下面的代码,但它没有用。

INSERT INTO ##Subclass_Season_AS 
exec sp_executesql @nSQL;

也尝试了,

set @strNewQuery ='SELECT '+@nSQL+' INTO ##temptablename '
exec @strNewQuery

你能帮忙吗?谢谢!

1 个答案:

答案 0 :(得分:0)

您可能想尝试将INTO语句放在动态查询中。

SET @nSQL = EXECUTE ('SELECT non empty {
[Coded Season].[Coded Season].[Coded Season] *
[Season].[Season].[Season] *
[Product].[Subclass].[Subclass] *
[Product].[Subclass Id].[Subclass Id]
} ON ROWS,{
[Measures].[Pl No of Range Opts]
} ON COLUMNS
INTO ##temptablename
FROM RP_C0') AT AS_T_RP_5900_Admin