I have created a temprary table variable, which I then need to pivot:
Declare @TempTable TABLE(
Name varchar(150),
CloseDate Date,
Revenue Float)
.... <add data to it> .....
SET @cols = STUFF((SELECT distinct ',' + QUOTENAME(CloseDate)
FROM @TempTable
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
,1,1,'')
set @query = 'SELECT Name, ' + @cols + ' from
(select
t.Name,
t.CloseDate,
t.Revenue
from @TempTable as t
) x
pivot
(SUM(Revenue)
for CloseDate in (' + @cols + ')
) p '
execute(@query)
However, I am getting this error:
Must declare the scalar variable "@TempTable".
When I test the @TempTable variable using a normal SELECT it works fine:
SELECT * from @TempTable
How can I reference the variable successfully in the query string?
'proper'temp tables ??