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; -- continue 句柄执行了指定的工作,并继续存储过程的执行,exit句柄将从最近的begin...end 代码中退出。 -- for sqlstate '02000'等价于FOR NOT FOUND 语句. -- 其他选项包括SQLwarning和sqlexception 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; -- 还支持 while eondition do -- ... -- end while; close c1; set largest_id=l_id; end $$ delimiter ;