MYSQL语法错误,意外的while_sym,期待END_OF_INPUT或&#39 ;;'

时间:2015-07-27 12:39:36

标签: mysql

我试图创建一个归档事件,我想出的是这样的:

SET @Date = NULL;
SET @SEDOL = NULL;
CREATE EVENT IF NOT EXISTS Archive_Event
ON SCHEDULE EVERY 1 DAY
DO
SELECT @Date = Date,@SEDOL = SEDOL
from daily.analytics
where   Date =  (SELECT MIN(Date)
                 from daily.analytics 
                 WHERE Date < date_sub(NOW(), INTERVAL 21 DAY))
order by SEDOL desc limit 1

WHILE @date is not null
begin
    begin transaction
    -- Adds old line to archive
    insert into daily.Archive_analytics
        select * from daily.analytics where Date = @Date AND SEDOL = @SEDOL
    -- Deletes old line from main table
    delete from daily.analytics where Date = @Date AND SEDOL = @SEDOL
    commit transaction
    -- Find the next greater minimum value
    SELECT @Date = Date,@SEDOL = SEDOL
    from daily.analytics
    where   Date =  (SELECT MIN(Date)
                     from daily.analytics 
                     WHERE Date < date_sub(NOW(), INTERVAL 21 DAY))
     order by SEDOL desc
     limit 1
END WHILE
END

搜索网络似乎或多或少是正确的,但是我的WHILE语句正在获得&#34;语法错误,意外的while_sym,期待END_OF_INPUT或&#39 ;;&#39;&#34;而且我不太清楚为什么。

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

事实证明问题是由于分隔符和代码中有多个块。没有语法错误的版本如下所示:

SET @Date = NULL;
SET @SEDOL = NULL;

delimiter |
CREATE EVENT IF NOT EXISTS daily.Archive_Event_HOUR
ON SCHEDULE EVERY 1 DAY
DO
    begin
    SELECT @Date = Date,@SEDOL = SEDOL
    from daily.analytics
    where   Date =  (SELECT MIN(Date)
                     from daily.analytics 
                     WHERE Date < date_sub(NOW(), INTERVAL 21 DAY))
    order by SEDOL desc limit 1;

    WHILE @Date is not null DO
        start transaction;
        -- Adds old line to archive
        insert into daily.Archive_analytics
            select * from daily.analytics where Date = @Date AND SEDOL = @SEDOL;
        -- Deletes old line from main table
        delete from daily.analytics where Date = @Date AND SEDOL = @SEDOL;
        commit;
        -- Find the next greater minimum value
        SELECT @Date = Date,@SEDOL = SEDOL
        from daily.analytics
        where   Date =  (SELECT MIN(Date)
                         from daily.analytics 
                         WHERE Date < date_sub(NOW(), INTERVAL 21 DAY))
         order by SEDOL desc
         limit 1;
    END WHILE;
END|
delimiter ;