When running this MS SQL on a table data binding I receive an error:
GatewayException: The statement did not return a result set.
SQLServerException: The statement did not return a result set.
The query runs correctly in Management Studio and returns results. Suggestions? Do I need to run from a stored procedure?
DECLARE @DynamicPivotQuery AS NVARCHAR(MAX)
DECLARE @ColumnName AS NVARCHAR(MAX)
SELECT CAST(TimeComplete AS DATE) [Date],
DATEPART(hour,TimeComplete) [Hour], Count(1) [Production Count]
INTO #PivotData
FROM Table
GROUP BY CAST(TimeComplete AS DATE), DATEPART(hour,TimeComplete)
--Get distinct values of the PIVOT Column
SELECT @ColumnName= ISNULL(@ColumnName + ',','')
+ QUOTENAME([Date])
FROM (SELECT DISTINCT [Date] FROM #PivotData) AS Dates
--Prepare the PIVOT query using the dynamic
SET @DynamicPivotQuery =
N'SELECT [Hour], ' + @ColumnName + '
FROM #PivotData
PIVOT(SUM( [Production Count] )
FOR [Date] IN (' + @ColumnName + ')) AS PVTTable'
--Execute the Dynamic Pivot Query
EXEC sp_executesql @DynamicPivotQuery