2

I'm trying to create an archiving event, and what I came up with look like this:

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

Searching the web this seems to be more or less correct, however my WHILE statement is getting a "Syntax Error, unexpected while_sym, expecting END_OF_INPUT or ';'" and I'm not quite sure why.

Would greatly appreciate any help.

3
  • Where is the end for the begin after the while ? Commented Jul 27, 2015 at 12:44
  • I guess you should have an end inside WHILE loop for begin Commented Jul 27, 2015 at 12:44
  • Do you guys mean the "begin transaction"? Commented Jul 27, 2015 at 13:41

1 Answer 1

1

So it turns out the problem was due to delimiters and having multiple blocks in the code. The version without syntax errors looks like this:

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 ;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.