SQL游标语法错误

时间:2016-12-22 12:05:23

标签: mysql cursor

我一直在解决这个问题。在SO上找不到任何答案,所以如果有人能帮助我,我会很高兴!

以下查询导致suntax错误。有什么问题的线索?

BEGIN
    DECLARE cur CURSOR FOR
    SELECT * FROM objects
    WHERE present_in_last_scrape = FALSE;

    DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;

    OPEN cur;
        DECLARE done INT DEFAULT 0; 
        read_loop: LOOP
            FETCH cur INTO record;
            IF done THEN
                LEAVE read_loop;
            END IF;
            UPDATE lifetimes SET end_time=extract(epoch from now()) WHERE object_id=record.object_id AND end_time IS NULL;
        END LOOP;
    CLOSE cur;
END

语法错误:

SQL Error [1064] [42000]: 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 'DECLARE cur CURSOR FOR
    SELECT * FROM objects
    WHERE present_in_last_scrap' at line 2
SQL Error [1064] [42000]: 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 'DECLARE cur CURSOR FOR
    SELECT * FROM objects
    WHERE present_in_last_scrap' at line 2
  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 'DECLARE cur CURSOR FOR
    SELECT * FROM objects
    WHERE present_in_last_scrap' at line 2
  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 'DECLARE cur CURSOR FOR
    SELECT * FROM objects
    WHERE present_in_last_scrap' at line 2

1 个答案:

答案 0 :(得分:0)

谢谢大家的帮助! epoch的东西确实是postgresql,已经改变了。 @solarflare与create_procedure的评论是对的,最终完成了这个伎俩!

CREATE PROCEDURE set_end_time (out done int)
BEGIN   
    DECLARE cur CURSOR FOR
        SELECT * FROM objects
        WHERE present_in_last_scrape = FALSE;

    OPEN cur;       
        LOOP
            UPDATE lifetimes SET end_time=UNIX_TIMESTAMP(NOW()) WHERE hemnet_id=cur.hemnet_id AND end_time IS NULL;
        END LOOP;
    CLOSE cur;
END