以下作品:
DECLARE @cols AS NVARCHAR(MAX),
@query AS NVARCHAR(MAX);
SET @cols =
STUFF
(
(
SELECT ',' + QUOTENAME(b."NewName")
FROM (
SELECT myKey,
win = SUM(win)
FROM xxx.dbo.yyy
GROUP BY myKey
) x
INNER JOIN #NameSwitch b ON
x.myKey = b.myKey
ORDER BY x.win DESC
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
,1,1,'');
SET @query =
'SELECT [Measure],' + @cols + '
FROM
(
SELECT [NewName], [Measure], [Amount]
FROM #UnpivottedData x
) x
PIVOT
(
SUM(Amount)
FOR [NewName] in (' + @cols + ')
) p ';
EXECUTE(@query);
但问题是我想将结果输入到一个临时表中#xxx
,然后以后就可以通过SELECT * FROM #xxx
.
在运行上述之前我需要CREATE
#xxx使用吗?dynamic sql
如果是这样,任何人都可以向我指出完成此操作的示例的方向。