简体   繁体   English

尝试在不预先定义临时表的情况下将动态SQL子查询结果放入SQL-Server 2008临时表

[英]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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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