0
DECLARE @cols AS NVARCHAR(MAX);
DECLARE @query AS NVARCHAR(MAX);
select @cols = STUFF((SELECT distinct ',' +
QUOTENAME(hi)
FROM #hello
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
, 1, 1, '');
--select @cols
上述代碼出於某種原因沒有輸入日期到#storage
臨時表中? 這是爲什麼發生?動態查詢不寫入臨時表
select @query = 'SELECT *
into #storage
FROM (
SELECT *
FROM #hello) up
PIVOT (MAX(AFEAmount) FOR hi IN (' + @cols + ')) AS pvt'
exec(@query)