3

I had to built a dynamic PIVOT in SQL Server as described in this article using the following script:

DECLARE @DynamicPivotQuery AS NVARCHAR(MAX)
DECLARE @ColumnName AS NVARCHAR(MAX)

--Get distinct values of the PIVOT Column 
SELECT @ColumnName= ISNULL(@ColumnName + ',','') 
       + QUOTENAME(Course)
FROM (SELECT DISTINCT Course FROM #CourseSales) AS Courses

--Prepare the PIVOT query using the dynamic 
SET @DynamicPivotQuery = 
  N'SELECT Year, ' + @ColumnName + '
    FROM #CourseSales
    PIVOT(SUM(Earning) 
          FOR Course IN (' + @ColumnName + ')) AS PVTTable'
--Execute the Dynamic Pivot Query
EXEC sp_executesql @DynamicPivotQuery

It works great and it returns a dynamic table as expected.

However, I would like to be able to select it in an other query but can not find a proper way to do so:

  • A view does not work as it does not accept variables
  • A table valued function does not work as I don't know in advance the structure of the output table
  • A stored procedure does not work as I can not use it in a SELECT query

What would be the best solution to save this dynamic pivot query and be able to SELECT it afterwards?

Thank you

Sylvain

2
  • You could create this as function to build your @sql variable and to return it for you, then in your main query you can execute it there
    – Veljko89
    Feb 17, 2017 at 8:19
  • That is, if your main query contains temp tables (or temp tables where just for example)?
    – Veljko89
    Feb 17, 2017 at 8:19

2 Answers 2

0

Oh. You can use temp table, my friend. The first, check the exists of temp table. And remember drop it at the end of query.

0

You could try to use the stored procedure and in that sp use the SELECT INTO in the dynamic pivot query and store in a temporary table (#myDynamicPivot).

Then you can use the temporary table in your next selects statements. Make sure you drop your table after using it.

Edit

I would consider moving that data that the pivot table is created against to a data warehouse and create an analysis database over it. This way you can query using the dimensions and the data will be much faster and it will be more optimized for storage and query.

2
  • Thank you. The dynamic pivot relies on a table which will be regularly updated and the SELECT query on it would be very oftenly used. It will mean that the temp table will be recreated and dropped very often . Would a temp table the best solution in that case, couldn't it lead to poor performance and higher disk activity?
    – Sylvain C.
    Feb 17, 2017 at 10:41
  • It depends of the size of the table. If you have 1m rows in it and drop and create it very often then it might be a performance problem. The temporary tables are created in the temp db. It will result in a larger log file for temp db database. Maybe add a job to shrink it regularly. Also, if you have enough RAM on the SQL Server it will help. Feb 17, 2017 at 11:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.