MySQL存储过程详解,可参考:http://blog.youkuaiyun.com/liang_k/article/details/8953238
drop procedure if exists dr_custody;
delimiter //
create procedure dr_custody()
begin
#定义 变量
declare cusId varchar(32);
#这个用于处理游标到达最后一行的情况
declare tag int default 0;
#声明游标cur(cur是个多行结果集)
declare cur cursor for select cus_id from yjr.detection_record group by cus_id;
#设置一个终止标记
declare continue handler for sqlstate '02000' set tag = 1;
#打开游标
open cur;
drop table if exists yjr.dr_temp;
create table yjr.dr_temp select * from yjr.detection_record where 1 = 2;
fetch cur into cusId;
while tag <> 1 do
insert into yjr.dr_temp select * from yjr.detection_record where cus_id = cusId order by dr_tag desc limit 1;
fetch cur into cusId;
end while;
#关闭游标
close cur ;
select * from yjr.dr_temp;
end;
// delimiter ;
#调用存储函数dr_custody
call yjr.dr_custody();
02000 | 发生下述异常之一:
|
02000描述来自http://blog.youkuaiyun.com/cangyingaoyou/article/details/7402243