[英]MySql stored procedure error [ERROR 1338 (42000): Cursor declaration after handler declaration]
我正在閱讀有關存儲過程的文章,這是代碼:
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 ;
現在工作正常。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.