自己写的一个mysql存储过程如下:

BEGIN
DECLARE _did bigint(20);
DECLARE _count int;
DECLARE s1 int;
DECLARE cur_1 CURSOR FOR select id from info; /** 声明游标,并将查询结果存到游标中 **/
/** 获取查询数量 **/
SELECT count(id) into _count from info;
SET s1=1;
START TRANSACTION;#开启事务
open cur_1;#打开游标
while s1<_count+1 DO
FETCH cur_1 INTO _did;
-- 嵌套使用游标
BEGIN
#声明变量
DECLARE token int DEFAULT 0;
DECLARE _d int;
DECLARE _t int;
DECLARE _bdate datetime;
#定义一个游标
DECLARE cur_2 CURSOR FOR
select rr.da,rr.ts from rr left join info di on di.r_id = rr.id where di.id = _did;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET token=1;
#开始循环游标
open cur_2;
FETCH cur_2 INTO _d,_t; -- 获取数据
while token<>1 DO
SET _bdate=DATE_SUB(now(),INTERVAL _d day);
BEGIN
DECLARE _uid bigint(20);
DECLARE done2 int DEFAULT 0;
DECLARE cur_3 CURSOR FOR select uid from u_bind where d_id=_did;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done2=1;
open cur_3;
while done2<>1 DO
FETCH cur_3 INTO _uid;
BEGIN
DECLARE _dubid bigint(20);
DECLARE _begintime datetime;
DECLARE _finishtime datetime;
DECLARE swork int DEFAULT 0;
DECLARE cur_8 CURSOR FOR select id,bdate,edate from u_bind where d_id=_did and u_id=_uid;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET swork=1;
open cur_8;
FETCH cur_8 INTO _dubid,_begintime,_finishtime;
if(_finishtime<now()) THEN #时间小于当前时间
update u_bind set sts=2 where id =_dubid;
ELSEIF(_begintime>now()) THEN
update u_bind set sts=0 where id =_dubid;
ELSE
update u_bind set sts=1 where id =_dubid;
while swork<>1 DO
SET swork = 0; #如果没有set swork=0的话 默认执行内层循环标记swork=1就会终止外层的循环 也就是只能执行一次操作就会推出。
FETCH cur_8 INTO _dubid,_begintime,_finishtime;
end while; #结束循环
END IF;#结束if
close cur_8; #关闭游标
END;

BEGIN
DECLARE _inid bigint(20);
DECLARE _finistime datetime;
DECLARE iwork int DEFAULT 0;
DECLARE cur_9 CURSOR FOR select id,e_date from plan where d_id=_did and u_id=_uid;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET iwork=1;
open cur_9;
FETCH cur_9 INTO _inid,_finistime;
if(_finistime<now()) THEN #时间小于当前时间
update plan set sts=2 where id =_inid;
ELSE
update plan set sts=1 where id =_inid;
while iwork<>1 DO
SET iwork=0;
FETCH cur_9 INTO _inid,_finistime;

end while;
END IF;
close cur_9;
END;

BEGIN
DECLARE _id bigint(20);
DECLARE _dp double;
DECLARE _sp double;
DECLARE _bvalue VARCHAR(50);
DECLARE _checkdate datetime;
DECLARE _rcount int;
DECLARE done3 int DEFAULT 0;
DECLARE cur_4 CURSOR FOR
select id,dp,sp,check_date from b_record
where u_id=_uid and level<>0 and level<>2 and check_date between _bdate and now()
order by check_date desc;

DECLARE CONTINUE HANDLER FOR NOT FOUND SET done3=1;
open cur_4;
select count(id) INTO _rcount from b_record
where level<>0 and level<>2 and check_date between DATE_SUB(now(),INTERVAL _d day) and now()
order by check_date desc;
if (_rcount>=_t) THEN
while done3<>1 DO
FETCH cur_4 INTO _id,_dp,_sp,_checkdate;
if(_id IS NOT NULL)&&(_dp IS NOT NULL)&&(_sp IS NOT NULL)&&(_checkdate IS NOT NULL) THEN
SET _bvalue=CONCAT(_dp,',',_sp);
BEGIN
DECLARE _birthday datetime;
DECLARE _purl VARCHAR(255);
DECLARE _sex int;
DECLARE _nameCh VARCHAR(20);
DECLARE _sts int;
DECLARE _begindate datetime;
DECLARE _age int;
DECLARE done int DEFAULT 0;
DECLARE cur_5 CURSOR FOR
select ui.birthday,ui.url,ui.sex,dub.name_ch,ud.label,ip.sts,ip.begin_date from info ui
left join u_bind dub on ui.u_id = dub.u_id
left join detail ud on ui.u_id = ud.u_id and dub.d_id = ud.d_id
left join plan ip on ui.u_id = ip.u_id
where ui.u_id=_uid;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done=1;
open cur_5;
while done<>1 DO
FETCH cur_5 INTO _birthday,_purl,_sex,_nameCh,_label,_sts,_begindate;
SET _age=year(now())-year(_birthday);
insert into storage(u_id,d_id,item,`value`,c_date,name,age,sex,url,label,sts,i_date,`status`)
VALUES (_uid,_did,1,_bvalue,_checkdate,_nameCh,_age,_sex,_purl,_label,_sts,_begindate,0);

SET done = 0;
FETCH cur_5 INTO _birthday,_purl,_sex,_nameCh,_label,_sts,_begindate;
end while;
close cur_5;
END;
END IF;
SET done3 = 0;
FETCH cur_4 INTO _id,_dp,_sp,_checkdate;
end while;
END IF;
close cur_4;
END;

BEGIN
DECLARE _sid bigint(20);
DECLARE _bsug double;
DECLARE _timepoint int;
DECLARE _scount int;
DECLARE _svalue VARCHAR(50);
DECLARE _checkdate datetime;
DECLARE token1 int DEFAULT 0;
DECLARE cur_6 CURSOR FOR
select sr.id,sr.sug,sr.point,sr.check_date
from s_rec sr
left join c_rule bcr on sr.r_id = bcr.ru_id
where bcr.level<>0 and sr.u_id =_uid and sr.c_date between _bdate and now() order by sr.c_date desc;

DECLARE CONTINUE HANDLER FOR NOT FOUND SET token1=1;
open cur_6;
select count(sr.id) INTO _scount from s_rec sr
left join c_rule bcr on sr.c_id = bcr.r_id
where bcr.level<>0 and sr.u_id =_uid and sr.c_date between _bdate and now() order by sr.c_date desc;
if (_scount>=_t) THEN
while token1<>1 DO
FETCH cur_6 INTO _sid,_bsug,_timepoint,_checkdate;
if(_sid IS NOT NULL)&&(_bsug IS NOT NULL)&&(_timepoint IS NOT NULL)&&(_checkdate IS NOT NULL) THEN
SET _svalue=CONCAT(_bsug,',',_timepoint);#连接字符串

END IF;
SET token1 = 0;
FETCH cur_6 INTO _sid,_bsug,_timepoint,_checkdate;
end while;
END IF;
close cur_6;
END;

SET done2 = 0;
FETCH cur_3 INTO _uid;
end while;
close cur_3;
END;
SET token = 0;
FETCH cur_2 INTO _d,_t;
end while;
close cur_2;
END;
SET s1=s1+1;
end while;
close cur_1;
delete from temp;
insert into temp select * from storage;

COMMIT; -- 事务提交
END

参考网址:

http://www.jb51.net/article/32139.htm

http://blog.csdn.net/wq7570875/article/details/25136625

http://blog.csdn.net/zhanglu0223/article/details/47701935

mysql存储过程游标嵌套循环的更多相关文章

  1. MySQL存储过程 游标

    MySQL存储过程  游标 如何在存储过程中使用MySQL游标来遍历SELECT语句返回的结果集 MySQL游标简介 要处理存储过程中的结果集,请使用游标.游标允许您迭代查询返回的一组行,并相应地处理 ...

  2. MySQL 存储过程/游标/事务

    将会用到的几个表 mysql> DESC products; +------------+--------------+------+-----+---------+-------------- ...

  3. php调用mysql存储过程游标

    <?php $dbtype = 'mysql'; $host = 'localhost'; $dbname = 'test'; $dsn = "$dbtype:host=$host;d ...

  4. mysql存储过程游标加计划任务事件调度器

    存储过程加事件调度器 -- 存储过程 (多个)游标的使用 临时表的使用(让执行时间从一个小时降低到5分钟)DELIMITER $$ DROP PROCEDURE IF EXISTS `eval_cal ...

  5. mysql 存储过程 游标嵌套

    基本表temp 包括 name, type, sendCity, getCity 分别对应物流送货司机名, 倒车的第几段, 发货城市, 收货城市 表结构 -- -------------------- ...

  6. MySQL 存储过程游标

    一.创建游标 游标用declare语句创建.如下面的例子所示: create procedure test2() begin declare cursorTest cursor for select ...

  7. mysql存储过程 --游标的使用 取每行记录 (多字段)

    delimiter $ create PROCEDURE phoneDeal() BEGIN DECLARE id varchar(64); -- id DECLARE phone1 varchar( ...

  8. mysql 存储过程 -- 游标的使用(备忘)

    BEGIN ; DECLARE f_ratio FLOAT DEFAULT 0.8; ); ); DECLARE i_statDate DATE; DECLARE i_accumulateCount ...

  9. mysql 存储过程 游标的使用 与定义

    1.游标的作用及属性 游标的作用就是用于对查询数据库所返回的记录进行遍历,以便进行相应的操作:游标有下面这些属性: a.游标是只读的,也就是不能更新它: b.游标是不能滚动的,也就是只能在一个方向上进 ...

随机推荐

  1. C#基础回顾:正则表达式-转

    写在前面:本文根据笔者的学习体会结合相关书籍资料对正则表达式的语法和使用(C#)进行基本的介绍.适用于初学者. 摘要:正则表达式(Regular Expressions),相信做软件开发的朋友或多或少 ...

  2. SQLServer数据库降级方法详解

    右击数据库,有一个任务选项--扩展里面有一个生成脚本 设置脚本选项 高级 最下方"要编写的脚本类型" 选择 框架及数据 完成

  3. php一句话木马

    一句话木马就是只需要一行代码的木马,短短一行代码,就能做到和大马相当的功能. 为了绕过waf的检测,一句话木马出现了无数中变形,但本质是不变的:木马的函数执行了发送的命令. 通过GET .POST . ...

  4. python中将图片从客户端(client)推到(POST)到服务器端(server)的方法

    从客户端推json到服务器端的工作可以用flask很容易做到,那么需要推送图片的话可以先将图片存到json中再进行操作. 服务器端 from flask import request, Flask i ...

  5. supervisor的command执行两条命令

    如下supervisor的进程的comand配置参数只能写一个命令 1.要执行多条命令,可以写个sh文件包含多条命令,然后sh -x   xxxx.sh,但这样又多了一个文件, 2.把所有命令放在字符 ...

  6. [Tensorflow] Cookbook - Object Classification based on CIFAR-10

    Convolutional Neural Networks (CNNs) are responsible for the major breakthroughs in image recognitio ...

  7. 10享元模式Flyweight

    一.什么是享元模式 Flyweight模式也叫享元模式,是构造型模式之 一,它通过与其他类似对象共享数据来减小内存 占用. 二.享元模式的结构 三.享元模式的角色和职责 抽象享元角色: 所有具体享元类 ...

  8. Mac OSX安装启动 zookeeper

    安装 zookeeper支持brew安装 ➜ ~ brew info zookeeper zookeeper: stable (bottled), HEAD Centralized server fo ...

  9. JavaScript隐藏的坑一,隐式调用toString

    最近在重新学习JavaScript,看动态原型对象的时候,打印了两个用同一个构造函数生成的对象,但是打印结果却不一样,请看代码: var box1=new Box(); console.log(box ...

  10. - Fractal(3.4.1)

    C - Fractal(3.4.1) Time Limit:1000MS    Memory Limit:30000KB    64bit IO Format:%I64d & %I64u Su ...