动态查询结果存入临时表或表变量

发布于 2024-11-09 16:17:00 字数 250 浏览 0 评论 0原文

我有一个使用 sp_executesql 生成结果集的存储过程,结果中的列数可能有所不同,但采用 Col1 Col2 Col3 等形式。

我需要将结果放入临时表或表变量中,所以我可以使用它。问题是我需要定义临时表的列,但我无法使用 sp_executesql 动态执行此操作,因为执行命令后临时表的范围会丢失。

我曾考虑过使用全局临时表的想法,因为范围允许动态创建它,但是,全局临时表很有可能通过此过程的并发执行进行更新。

有什么想法吗?

I have a stored procedure that uses sp_executesql to generate a result set, the number of columns in the result can vary but will be in the form of Col1 Col2 Col3 etc.

I need to get the result into a temp table or table variable so I can work with it. The problem is I need to define the columns of the temp table, which I cant do dynamically using sp_executesql as the scope of the temp table is lost after the command is executed.

I have toyed with the idea of using Global Temp tables, as the scope allows it to be created dynamically, however, there is a very good chance the Global Temps would get updated by the concurrent executions of this process.

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

南冥有猫 2024-11-16 16:17:01

我在这篇文章中的 @SQLMenace 的帮助下找到了一个适合我的解决方案 T-SQL 动态 SQL 和临时表

简而言之,我需要首先在普通 SQL 中创建一个 #temp 表,然后我可以使用进一步的动态 SQL 语句更改结构。在此示例中,@colcount 设置为 6。当我实现此操作时,这将由另一个存储过程确定。

IF object_id('tempdb..#myTemp') IS NOT NULL
DROP TABLE #myTemp

CREATE TABLE #myTemp (id int IDENTITY(1,1) )
DECLARE @cmd nvarchar(max)
DECLARE @colcount int
SET @colcount = 6
DECLARE @counter int
SET @counter = 0
WHILE @counter < @colcount
    BEGIN
      SET @counter = @counter + 1
      SET @cmd = 'ALTER TABLE #myTemp  ADD col' + CAST(@counter AS varchar(4)) + ' NVARCHAR(MAX)'
      EXEC(@cmd)
    END

INSERT INTO #myTemp 
EXEC myProc @param1, @param2, @param3

SELECT * FROM #myTemp

I have found a solution that works for me with the help of @SQLMenace in this post T-SQL Dynamic SQL and Temp Tables

In short, I need to create a #temp table in normal SQL first, then I can alter the structure using further dynamic SQL statements. In this example @colcount is set to 6. This will be determined by another stored proc when I implement this.

IF object_id('tempdb..#myTemp') IS NOT NULL
DROP TABLE #myTemp

CREATE TABLE #myTemp (id int IDENTITY(1,1) )
DECLARE @cmd nvarchar(max)
DECLARE @colcount int
SET @colcount = 6
DECLARE @counter int
SET @counter = 0
WHILE @counter < @colcount
    BEGIN
      SET @counter = @counter + 1
      SET @cmd = 'ALTER TABLE #myTemp  ADD col' + CAST(@counter AS varchar(4)) + ' NVARCHAR(MAX)'
      EXEC(@cmd)
    END

INSERT INTO #myTemp 
EXEC myProc @param1, @param2, @param3

SELECT * FROM #myTemp
完美的未来在梦里 2024-11-16 16:17:01

您是否有任何原因不能执行以下操作:

SELECT *
INTO #MyTempTable
FROM MyResultSet

SELECT INTO 不需要显式字段列表。

IS there any reason you can't do something like:

SELECT *
INTO #MyTempTable
FROM MyResultSet

SELECT INTO doesn't require an explicit field list.

青瓷清茶倾城歌 2024-11-16 16:17:01

您可以使用全局临时表,其名称由创建进程的 SPID 进行“唯一化”。这可以让您避免破坏其他连接创建的其他全局临时表。

只要确保完成后清理它们......:)

You can use global temp tables whose names are 'uniquified' by the SPID of the creating process. This can allow you to avoid stomping on other global temp tables created by other connections.

Just make sure to clean them up when you're done... :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文