尝试将动态SQL子查询结果放入SQL- server 2008临时表中,而不需要预先定义临时表

时间:2022-03-16 02:03:16

I´m trying to get my result into a temp table but can´t get it to work.

我´m试图把结果变成一个临时表,但不能让它´工作。

DECLARE @query nvarchar(max)
SET @query = N'SELECT * INTO ##TmpTbl FROM (SELECT * FROM Tbl1)'
EXEC(@query)

What am i doing wrong?

我做错了什么?

NOTE: I can NOT pre define temp table/table variable because of the actual question being run is a pivot question without pre defined columns in it´s result.

注意:我不能预先定义临时表和表变量,因为实际的运行是一个主问题没有以前´s中定义的列的结果。

1 个解决方案

#1


4  

You are missing the alias on the subquery:

您正在丢失子查询上的别名:

DECLARE @query nvarchar(max)
SET @query = N'SELECT * 
               INTO ##TmpTbl 
               FROM (SELECT * FROM Tbl1) src'  <--- you need an alias
EXEC(@query)

#1


4  

You are missing the alias on the subquery:

您正在丢失子查询上的别名:

DECLARE @query nvarchar(max)
SET @query = N'SELECT * 
               INTO ##TmpTbl 
               FROM (SELECT * FROM Tbl1) src'  <--- you need an alias
EXEC(@query)