动态sql输出到临时表

时间:2017-05-10 14:30:04

标签: sql sql-server dynamic output

我可以成功运行以下代码来转置数据集,并希望将输出存储在临时表中,这样我就可以在下面的脚本之外运行一些代码,例如'select * from #table',并查看结果。任何帮助将不胜感激。

数据:

TimeSeconds TagID Value
1378700244  A1    3.75
1378700245  A1    30
1378700304  A1    1.2
1378700305  A2    56
1378700344  A2    11
1378700345  A3    0.53
1378700364  A1    4
1378700365  A1    14.5
1378700384  A1    144
1378700384  A4    10

代码:

DECLARE @cols NVARCHAR(MAX), @sql NVARCHAR(MAX)

SET @cols = STUFF((SELECT DISTINCT ',' + QUOTENAME(TagID)
            FROM Table1
            ORDER BY 1
            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)'),1,1,'')

SET @sql = 'SELECT TimeSeconds, ' + @cols + '
              FROM
            (
              SELECT TimeSeconds, TagID, Value
                FROM table1
            ) s
            PIVOT
            (
              MAX(Value) FOR TagID IN (' + @cols + ')
            ) p'

EXECUTE(@sql)

2 个答案:

答案 0 :(得分:0)

嗯,这是一个问题,因为您事先并不知道列数 这意味着您无法使用create table定义临时表 我提出了一个非常丑陋的解决方法,或许其他人能够以更优雅的方式做到这一点 我的想法是使用select...into在动态sql脚本中创建一个全局临时表,然后使用另一个select into从该表创建一个本地临时表并删除全局表。 (当然,如果您对全局表感到满意,这一步骤是多余的)

DECLARE @cols NVARCHAR(MAX), @sql NVARCHAR(MAX)

SET @cols = STUFF((SELECT DISTINCT ',' + QUOTENAME(TagID)
            FROM Table1
            ORDER BY 1
            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)'),1,1,'')

-- Note I'va added ' INTO ##TEMP'
SET @sql = 'SELECT TimeSeconds, ' + @cols + ' INTO ##TEMP 
              FROM
            (
              SELECT TimeSeconds, TagID, Value
                FROM table1
            ) s
            PIVOT
            (
              MAX(Value) FOR TagID IN (' + @cols + ')
            ) p'

EXECUTE(@sql)


-- Create the local temporary table from the data in the global one
SELECT * INTO #Temp 
FROM ##TEMP
-- Drop the global temporary table
DROP TABLE ##TEMP

请注意,如果多个用户可能同时运行此脚本,那么由于使用了全局临时表,这不是一个好主意。

答案 1 :(得分:0)

IF OBJECT_ID('Tempdb..#Temp') IS NOT NULL
DROP TABLE #Temp

;With cte(TimeSeconds, TagID, Value)
AS
(
SELECT 1378700244,'A1',3.75 Union all
SELECT 1378700245,'A1',30   Union all
SELECT 1378700304,'A1',1.2  Union all
SELECT 1378700305,'A2',56   Union all
SELECT 1378700344,'A2',11   Union all
SELECT 1378700345,'A3',0.53 Union all
SELECT 1378700364,'A1',4    Union all
SELECT 1378700365,'A1',14.5 Union all
SELECT 1378700384,'A1',144  Union all
SELECT 1378700384,'A4',10   
)
SELECT * INTO #temp From cte

DECLARE @cols Nvarchar(max),
        @sql Nvarchar(max),
        @sql2 Nvarchar(max),
        @cols2 Nvarchar(max)


SET @cols = STUFF((SELECT DISTINCT ',' + 'ISNULL('+ TagID +',''0'' )'+' AS '+ QUOTENAME(TagID) FROM #temp
            ORDER BY 1
            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)'),1,1,'')


SET @cols2 = STUFF((SELECT DISTINCT ',' +  QUOTENAME(TagID) FROM #temp
            ORDER BY 1
            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)'),1,1,'')


SET @sql = 'SELECT TimeSeconds, ' + @cols + ' INTO #Temp1
              FROM
            (
              SELECT TimeSeconds, TagID, Value
                FROM #Temp
            ) s
            PIVOT
            (
              MAX(Value) FOR TagID IN (' + @cols2 + ')
            ) p'

PRINT @sql

SET @sql2='SELECT TimeSeconds,'+ @cols + ' FROM  #temp1'

PRINT @sql2

EXECUTE(@sql)

EXECUTE(@sql2)