mysql - SQL游标语法错误

标签 mysql cursor

我一直在为这个问题伤脑筋。目前还找不到任何答案,所以如果有人能帮助我,我将不胜感激!

以下查询会导致 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

最佳答案

谢谢大家的帮助! 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

关于mysql - SQL游标语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41282832/

相关文章:

PHP MYSQL 插入数据时出错

python - 谷歌应用引擎分页和光标

php - mysql 用户访问被拒绝

mysql从一张表更新另一张表

intellij-idea - 按 Ctrl-S 时,IntelliJ IDEA 光标移动到行首

PHP MySQL 打印光标到网页?

android - 关还是不关?

c++ - mouse_event MOUSEEVENTF_LEFTDOWN 注册为选项卡而不是左键单击?

mysql - 创建mysql View 时是否应该在SELECT语句中引用多个表?

mysql - 我可以使用自制代码镜像数据库中的更改吗?