MySQL Stored Procedure INSERT INTO a temp table from CTE erroring
11:01 27 Jan 2026

I am creating a temp table in a stored procedure to hold the results of a CTE query. Without the temp table the query works fine. The code is below. I keep getting an error at the INSERT INTO line - and I can’t figure out why. Any insight would be greatly appreciated.

DROP TEMPORARY TABLE IF EXISTS SOL_Daily;

CREATE TEMPORARY TABLE SOL_Daily(

o_YEAR YEAR,

o_Month INT,

o_Day INT,

SolHours Decimal(4,1),

avgSolar Decimal(10,4)

);

WITH YMD_Data AS(

SELECT Month(ObsDate) as o_Month, Day(ObsDate) as o_Day, YEAR(ObsDate) as o_Year,

MAX(ObsTime) as mxTime,

MIN(ObsTime) as mnTime,

AVG(Solar) as avgSolar

FROM Daily

GROUP BY o_Year,o_Month,o_day

HAVING AVG(Solar) \> 100

ORDER BY o_Month, o_Day

)

INSERT INTO SOL_Daily(o_YEAR, o_Month, o_Day, SolHours, avgSolar) 

SELECT o_Year, o_Month, o_Day,

ROUND(TIMESTAMPDIFF(SECOND, mnTime, mxTime) / 3600, 1) AS SolHours,

avgSolar

FROM YMD_Data;
sql mysql stored-procedures