SQL statement did not return a result set error

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

I think you should put your stored procedure inside the Management Studio then call it from commands system.db.createSProcCall() system.db.execSProcCall()

Yep, you need to make this a stored procedure call. The JDBC drivers do not work with declaring variables or with multiple statements.