MySQL存储过程实例

 

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

发生下述异常之一:

  • SELECT INTO 语句或 INSERT 语句的子查询的结果为空表。

  • 在搜索的 UPDATE 或 DELETE 语句内标识的行数为零。

  • 在 FETCH 语句中引用的游标位置处于结果表最后一行之后。

02000描述来自http://blog.youkuaiyun.com/cangyingaoyou/article/details/7402243

 


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值