MySql存储过程错误[错误1338(42000):处理程序声明后的游标声明]

罗伯特·罗查

我正在阅读有关存储过程的文章,这是代码:

delimiter //

create procedure largest_order(out largest_id int) 
begin
  declare this_id int;
  declare this_amount float;
  declare l_amount float default 0.0;
  declare l_id int;

  declare done int default 0;
  declare continue handler for sqlstate '02000' set done = 1;
  declare c1 cursor for select orderid, amount from orders;

  open c1;
  repeat
    fetch c1 into this_id, this_amount;
    if not done then
      if this_amount > l_amount then
        set l_amount=this_amount;
        set l_id=this_id;
      end if;
    end if;
   until done end repeat; 
  close c1;

  set largest_id=l_id;

end
//

delimiter ;

我正在使用一个名为“ mydatabase”的简单数据库。运行完上面的代码后,它给了我这个错误:ERROR 1338 (42000): Cursor declaration after handler declaration什么是错误的,我该如何解决?

这是我第一次使用存储过程。

罗伯特·罗查

每个MySql文档

游标声明必须出现在处理程序声明之前,变量和条件声明之后。

所以我将代码更新如下:

delimiter //

create procedure largest_order(out largest_id int) 
begin
  declare this_id int;
  declare this_amount float;
  declare l_amount float default 0.0;
  declare l_id int;

  -- 1. cursor finished/done variable comes first
  declare done int default 0;
  -- 2. the curser declaration and select
  declare c1 cursor for select orderid, amount from orders;
  -- 3. the continue handler is defined last
  declare continue handler for sqlstate '02000' set done = 1;


  open c1;
  repeat
    fetch c1 into this_id, this_amount;
    if not done then
      if this_amount > l_amount then
        set l_amount=this_amount;
        set l_id=this_id;
      end if;
    end if;
   until done end repeat; 
  close c1;

  set largest_id=l_id;

end
//

delimiter ;

现在工作正常。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

MySql存储过程错误[错误1338(42000):处理程序声明后的游标声明]

来自分类Dev

MySQL存储过程的游标给出错误

来自分类Dev

存储过程-变量声明和类型错误

来自分类Dev

声明错误处理程序在MySQL中无效?

来自分类Dev

声明错误处理程序在MySQL中无效?

来自分类Dev

MySql存储过程循环游标-语法错误

来自分类Dev

SQL Server存储过程错误:需要声明表变量

来自分类Dev

必须声明存储过程中的标量变量错误

来自分类Dev

存储过程错误“必须声明标量变量”

来自分类Dev

(程序)未声明的错误

来自分类Dev

函数声明后,Javascript错误:“在参数列表后缺少”

来自分类Dev

类声明后编译错误,Main不“看到”该类

来自分类Dev

错误创建过程:声明并结束错误

来自分类Dev

Mysql存储过程错误

来自分类Dev

在mysql存储过程中声明变量

来自分类Dev

MySQL存储过程设置/声明变量问题

来自分类Dev

MySQL的存储过程与游标

来自分类Dev

MySQL的存储过程与游标

来自分类Dev

错误:声明未声明

来自分类Dev

PHP变量声明错误-MySQL

来自分类Dev

带有 order by 子句的游标声明抛出错误

来自分类Dev

存储过程错误PLS-00201:必须声明标识符'UTL_HTTP'

来自分类Dev

使用表参数创建存储过程时出现错误“必须声明标量变量”

来自分类Dev

顶级声明程序错误,但程序正常运行

来自分类Dev

MySQL存储过程执行错误

来自分类Dev

mysql存储过程创建错误

来自分类Dev

MySQL存储过程错误1418

来自分类Dev

语法错误:意外的令牌'='。在参数声明后应为')'或','。在野生动物园

来自分类Dev

声明后修改IEnumerator