【问题标题】:Cursors in MySQLMySQL中的游标
【发布时间】:2015-01-26 18:40:39
【问题描述】:

感谢您的关注和帮助!

我写了这个非常简单的光标,但由于某种原因它不想工作:

drop table if exists t1;
drop procedure if exists simple_loop;
create temporary table values ('chris'),('peter'),('brian'),('stewie'),('meg');
create procedure simple_loop()
begin
declare finished int default false;
declare n varchar(45);
declare c cursor for select name from t1;
declare continue handler for not found set finished = true;
open c;
c_loop: loop
    fetch c into n;
    --if finished then leave c_loop;
    select n;
end loop;
end

如您所见,我已经注释掉了结果完成后退出循环的行。这是因为整个过程脚本不会执行。

如果该行存在,则不会抱怨该行导致问题,而是结束:

...在'loop;end'附近使用正确的语法错误代码:1064

我不知道为什么会这样。 MySQL 文档似乎没有多大帮助。如果我还添加行'close c;'它也不起作用,但指出整个结尾是一个错误。

任何指针都非常感谢。

Ubuntu 上的 MySQL 版本为 5.5。

谢谢,克里斯

【问题讨论】:

标签: mysql cursor


【解决方案1】:

这似乎是固定的!我忘了终止那个 if 语句:

drop table if exists t1;
drop procedure if exists simple_loop;
create temporary table values ('chris'),('peter'),('brian'),('stewie'),('meg');
create procedure simple_loop()begin
declare finished int default false;
declare n varchar(45);
declare c cursor for select name from t1;
declare continue handler for not found set finished = true;
open c;
c_loop: loop
    fetch c into n;
    if finished then leave c_loop; end if;
    select n;
end loop;
end

查看之前注释掉的行:

if finished then leave c_loop; END IF;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-04
    • 2017-04-01
    • 1970-01-01
    相关资源
    最近更新 更多