虽然我能够在这些论坛中找到如何旋转这些数据的方法,但是我还没有找到将结果推送到临时表的方法,以便可以将其用于其他查询。代码如下。是否有可能使此输出填充临时表?
SET @cols = STUFF((SELECT distinct ',' + QUOTENAME(QT.QUESTION_DESC) FROM #QUES_TEMP QT GROUP BY QT.QUESTION_DESC FOR XML PATH(''), TYPE ).value('.', 'NVARCHAR(MAX)') ,1,1,'') set @query = 'SELECT EVAL_ID, AuditType, ' + @cols + ' into ##tmp from ( select QT.EVAL_ID, QT.AuditType, QT.SCORE, QT.QUESTION_DESC from #QUES_TEMP QT ) x pivot ( max(SCORE) for QUESTION_DESC in (' + @cols + ') ) p ' execute(@query); select * from ##tmp
您应该可以使用INTO子句。我在您的示例中添加了INTO。
SET @cols = STUFF((SELECT distinct ',' + QUOTENAME(QT.QUESTION_DESC) FROM #QUES_TEMP QT GROUP BY QT.QUESTION_DESC FOR XML PATH(''), TYPE ).value('.', 'NVARCHAR(MAX)') ,1,1,'') set @query = 'SELECT EVAL_ID, AuditType, ' + @cols + ' from ( select QT.EVAL_ID, QT.AuditType, QT.SCORE, QT.QUESTION_DESC into ##tmp from #QUES_TEMP QT ) x pivot ( max(SCORE) for QUESTION_DESC in (' + @cols + ') ) p ' execute(@query); SELECT * FROM ##tmp