20140808-mysql 存储过程

这篇文章是2014年8月8日在QQ空间记录的一篇工作心得,我把它给搬过来,也让更多人能够参考,学习,借鉴吧!

主要是使用mysql写一个存储过程吧。

修改前的存储过程:

BEGIN
  DECLARE temp_id int(11);
 DECLARE stopFlag int(11);
 DECLARE cnt int(11);
 DECLARE cnt1 int(11);
 DECLARE idx int(11);

 DECLARE noticereport_cursor CURSOR for select id from (select n1.id from supervisor_noticereport n1 where n1.project_id = project_id and n1.status = 0) a; 
 DECLARE CONTINUE HANDLER FOR NOT FOUND SET stopFlag = 1;
  select count(id) from (select n1.id from supervisor_noticereport n1 where n1.project_id = project_id and n1.status = 0 ) a into cnt;
 set idx = 0;
 open noticereport_cursor;
  REPEAT
  FETCH noticereport_cursor into temp_id;
   if idx < cnt then
   <span style="color:#ff0000;">SELECT r1.report_id,r1.user_id as last_user,d1.group_id as last_user_group,'supervisor_noticereport' as model,r1.time,1 as type,n1.name ,d1.name as username
   from supervisor_noticehistory r1, ess_userinfo u1, ess_department d1,supervisor_noticereport n1
   where r1.user_id = u1.id and d1.id = u1.department_id and r1.report_id = temp_id  and d1.group_id!=user_group_id  and n1.id = r1.report_id
   ORDER BY r1.id DESC 
   limit 1;</span>
   end if;
   set idx = idx + 1;
   until stopFlag = 1 end repeat;   
   close noticereport_cursor;
END

修改之后的存储过程:

BEGIN
  DECLARE temp_id int(11);
 DECLARE stopFlag int(11);
 DECLARE cnt int(11);
 DECLARE cnt1 int(11);
 DECLARE idx int(11);

 DECLARE noticereport_cursor CURSOR for select id from (select n1.id from supervisor_noticereport n1 where n1.project_id = project_id and n1.status = 0) a; 
 DECLARE CONTINUE HANDLER FOR NOT FOUND SET stopFlag = 1;
  select count(id) from (select n1.id from supervisor_noticereport n1 where n1.project_id = project_id and n1.status = 0 ) a into cnt;
 set idx = 0;
 open noticereport_cursor;
  REPEAT
  FETCH noticereport_cursor into temp_id;
   if idx < cnt then
   <span style="color:#ff0000;">SELECT * FROM (SELECT r1.report_id,r1.user_id as last_user,d1.group_id as last_user_group,'supervisor_noticereport' as model,r1.time,1 as type,n1.name ,d1.name as username
   from supervisor_noticehistory r1, ess_userinfo u1, ess_department d1,supervisor_noticereport n1
   where r1.user_id = u1.id and d1.id = u1.department_id and r1.report_id = temp_id  and n1.id = r1.report_id
   ORDER BY r1.id DESC 
   limit 1) a WHERE a.last_user_group!= user_group_id ;</span>   
   end if;
   set idx = idx + 1;
   until stopFlag = 1 end repeat;   
   close noticereport_cursor;
END 

我只想说语句位置摆放很关键!

哦,还有一个问题要说明,MySQL的5.5版本在游标里面有个bug,在5.6里面就修正了!!! 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值