database - 从 execute(@query) 中选择 * 进入#table

标签 database tsql azure-sql-database select-into

我创建了一个动态查询,它返回表中的列数:

set @query = 'select '+@cols+' from [Sample] '

现在我想通过执行这个查询来填充一个临时表,当我尝试这个的时候

select * into #table from execute(@query). 

我收到以下错误:

Incorrect syntax near the keyword 'execute'

Incorrect syntax near ')'

但是运行此命令会准确返回结果:execute(@query)

注意:我试过 OPENROWSET,sql-azure 不支持它。

如果还有其他解决方法,请提供帮助。

最佳答案

尝试使用 FQ 表名而不是#temptable:

 IF object_id('tempdb..temptable') IS NOT NULL DROP TABLE [tempdb].[dbo].[temptable] 
 DECLARE @query varchar(4000)
 SET @query = 'select '+ @cols +' into [tempdb].[dbo].[temptable] from [Sample]'

 EXECUTE (@query)

 SELECT * from [tempdb].[dbo].[temptable] 

结果请见SQLFiddle

关于database - 从 execute(@query) 中选择 * 进入#table,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33105246/

相关文章:

.net - 无法将文件 *.mdf 作为数据库附加

java - 用户的业务规则 Java 应用程序

sql-server - 我如何遍历每个记录并查看是否缺少 3 个要求中的任何一个?

sql-server - 在 Azure 中恢复数据库

azure - 降低数据工厂成本

sql-server - 为什么我的 Azure SQL 数据库性能会突然下降?

php - MySQL数据库性能

database - 如何填充 DynamoDB 表

sql - 为什么选择Top X Kill Performance

sql - sql中不存在如何使用