DECLARE @myTable TABLE(
Name VARCHAR(50),
Year INT,
Hours INT )
INSERT INTO @myTable *.... some values*
DECLARE @var INT
SET @var = 2015
DECLARE @DynamicPivot VARCHAR(MAX)
SET @DynamicPivot = 'SELECT * FROM
@myTable PIVOT( SUM(Hours) FOR Year IN (' + @var + ') ) AS PvtTable
EXEC sp_executesql @DynamicPivot
I am trying to create a dynamic table and rotate the table using a pivot table. The value in @var is the value that already exists in the dynamic table Year INT. Everything works fine except when I try to perform a dynamic rotation. This gives me an error that is @myTablenot declared, although I am running all the code at the same time. The problem is to be in the summary declaration, I really do not find the problem. Any ideas?
source
share