Good day StackOverflow
The table that I create from my dynamic sql can have any number of columns as it is a pivot table.
-- Pivot the table so we get the UW as columns rather than rows
DECLARE @SQL NVARCHAR(MAX)
SET @SQL = '
SELECT *
FROM #PreProcessed
PIVOT (SUM(Quotes)
FOR [UW] IN (' + @UWColumns + ')
) AS bob'
I run this code to run my dynamic sql.
EXEC sp_executesql @SQL,
N'@UWColumns nvarchar(MAX)',
@UWColumns
My question is, how do I store the resulting table? Especially when I don't know how many columns it will have or even what the columns will be called?
I tried the code below but it doesn't work
INSERT INTO #Temp
EXEC sp_executesql @SQL,
N'@UWColumns nvarchar(MAX)',
@UWColumns
Thanks everyone
The only way I could find around this problem was to do all of my calculations in the dynamic sql. Which meant I had to work on two tables.