开发者

Mysterious syntax error at SET in MySQL Stored Proc

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 cand开发者_Go百科idate;
            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 THEN on the line above it:

IF targetDate < candidate

should be

IF targetDate < candidate THEN

From the fine manual, the syntax for an IF statement is this:

IF search_condition THEN statement_list
    [ELSEIF search_condition THEN statement_list] ...
    [ELSE statement_list]
END IF

where optional things are, as usual, marked with brackets; there are not brackets around THEN so it is required.


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 THEN   -- You had missed THEN
                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 //

You have missed THEN after IF statement :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜