简体   繁体   中英

Trying to put a dynamic SQL subquery result into SQL-Server 2008 temp table without pre define the temp table

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

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.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM