I have a simple table that has two columns: an auto-increment int column called id and a date column called start. I’m trying to write a stored procedure that, given a value, will calculate the appropriate record to tie it to. The problem is that I’m getting a syntax error that doesn’t really tell me what’s going on. The error is:
Error Code: 1064. You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘SET finalResult = candidate;
Here is the code I’m using to create the procedure. Can anyone please tell me what I’m doing wrong?
delimiter //
CREATE PROCEDURE FindGCLDate(IN targetDate DATE)
BEGIN
DECLARE candidate DATE;
DECLARE finalResult DATE;
DECLARE loopComplete BOOLEAN;
DECLARE allDates CURSOR FOR SELECT * FROM gcl_dates;
DECLARE CONTINUE HANDLER FOR NOT FOUND
SET loopComplete = TRUE;
OPEN allDates;
myLoop : LOOP
FETCH allDates INTO candidate;
SELECT candidate;
IF targetDate < candidate
SET finalResult = candidate; -- this is the offending line
SET loopComplete = TRUE;
END IF;
IF loopComplete THEN
CLOSE allDates;
LEAVE myLoop;
END IF;
END LOOP myLoop;
SELECT finalResult;
END //
I know there is more wrong with the procedure — I haven’t defined an out parameter or returned anything yet. But I’m just wondering why I am getting this error.
Thanks!
You’re missing a
THENon the line above it:should be
From the fine manual, the syntax for an
IFstatement is this:where optional things are, as usual, marked with brackets; there are not brackets around
THENso it is required.