Mysql存储过程循环内嵌套使用游标示例代码
网络编程 2021-07-05 15:27www.168986.cn编程入门
本节主要介绍了Mysql存储过程循环内如何嵌套使用游标,详细实现如下,需要的朋友不要错过
BEGIN -- 声明变量 DECLARE v_addtime_begin varchar(13); DECLARE v_addtime_end varchar(13); DECLARE v_borrow_id int; DECLARE v_count int; DECLARE s1 int; / 声明游标,并将查询结果存到游标中 / DECLARE c_borrow CURSOR FOR SELECT ID from rocky_borrow WHERE BORROWTYPE = 2 AND PUBLISH_TIME >= UNIX_TIMESTAMP('2014-05-27') AND PUBLISH_TIME <= UNIX_TIMESTAMP('2014-07-30') ORDER by ID ASC; / 获取查询数量 / SELECT count(ID) INTO v_count from rocky_borrow WHERE BORROWTYPE = 2 AND PUBLISH_TIME >= UNIX_TIMESTAMP('2014-05-27') AND PUBLISH_TIME <= UNIX_TIMESTAMP('2014-07-30') ORDER by ID ASC; SET s1 = 1; -- 开始事务 START TRANSACTION; -- 打开游标 OPEN c_borrow; -- 循环游标 WHILE s1 < v_count+1 DO -- 遍历游标 FETCH c_borrow INTO v_borrow_id; SELECT t1.addtime INTO v_addtime_begin FROM (SELECT FROM rocky_b_tenderrecord bt WHERE BORROW_ID = v_borrow_id AND tender_type = 1 ORDER BY ID ASC) t1 GROUP BY t1.borrow_id; SELECT t1.addtime INTO v_addtime_end FROM (SELECT FROM rocky_b_tenderrecord bt WHERE BORROW_ID = v_borrow_id AND tender_type = 1 ORDER BY ID DESC) t1 GROUP BY t1.borrow_id; IF (v_addtime_begin IS NOT NULL) && (v_addtime_end IS NOT NULL) THEN -- 嵌套使用游标 BEGIN DECLARE v_id int; DECLARE v_user_id int; DECLARE v_type varchar(20); DECLARE v_total decimal(20,8) DEFAULT 0; DECLARE v_money decimal(20,8) DEFAULT 0; DECLARE v_use_money decimal(20,8) DEFAULT 0; DECLARE v_no_use_money decimal(20,8) DEFAULT 0; DECLARE v_collection decimal(20,8) DEFAULT 0; DECLARE v_to_user int(11); DECLARE v_remark VARCHAR(1000); DECLARE v_addtime varchar(13); DECLARE v_addip varchar(64); DECLARE v_first_borrow_use_money decimal(20,8) DEFAULT 0; DECLARE done VARCHAR(45) DEFAULT ''; DECLARE t_error int DEFAULT 0; DECLARE c_aountlog CURSOR FOR SELECT ID,USER_ID,TYPE,TOTAL,MONEY,USE_MONEY,NO_USE_MONEY,COLLECTION,TO_USER,REMARK,ADDTIME,ADDIP,FIRST_BORROW_USE_MONEY FROM ( SELECT ID,USER_ID,TYPE,TOTAL,MONEY,USE_MONEY,NO_USE_MONEY,COLLECTION,TO_USER,REMARK,ADDTIME,ADDIP,FIRST_BORROW_USE_MONEY FROM rocky_aountlog WHERE ADDTIME >= v_addtime_begin AND ADDTIME <= v_addtime_end AND (type = 'tender_cold' or type= 'repayment_deduct') ) t GROUP BY t.user_id HAVING count(t.user_id) > 1; DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = NULL; OPEN c_aountlog; FETCH c_aountlog INTO v_id,v_user_id,v_type,v_total,v_money,v_use_money,v_no_use_money,v_collection,v_to_user,v_remark,v_addtime,v_addip,v_first_borrow_use_money; WHILE (done IS NOT NULL) DO INSERT INTO rocky_aountlog_test2 (ACCOUNTLOG_ID,USER_ID,TYPE,TOTAL,MONEY,USE_MONEY,NO_USE_MONEY,COLLECTION,TO_USER,REMARK,ADDTIME,ADDIP,FIRST_BORROW_USE_MONEY,BORROW_ID) VALUES (v_id,v_user_id,v_type,v_total,v_money,v_use_money,v_no_use_money,v_collection,v_to_user,v_remark,v_addtime,v_addip,v_first_borrow_use_money,v_borrow_id); FETCH c_aountlog INTO v_id,v_user_id,v_type,v_total,v_money,v_use_money,v_no_use_money,v_collection,v_to_user,v_remark,v_addtime,v_addip,v_first_borrow_use_money; END WHILE; CLOSE c_aountlog; END; END IF; SET s1 = s1 + 1; END WHILE; CLOSE c_borrow; COMMIT; -- 事务提交 END
编程语言
- 如何快速学会编程 如何快速学会ug编程
- 免费学编程的app 推荐12个免费学编程的好网站
- 电脑怎么编程:电脑怎么编程网咯游戏菜单图标
- 如何写代码新手教学 如何写代码新手教学手机
- 基础编程入门教程视频 基础编程入门教程视频华
- 编程演示:编程演示浦丰投针过程
- 乐高编程加盟 乐高积木编程加盟
- 跟我学plc编程 plc编程自学入门视频教程
- ug编程成航林总 ug编程实战视频
- 孩子学编程的好处和坏处
- 初学者学编程该从哪里开始 新手学编程从哪里入
- 慢走丝编程 慢走丝编程难学吗
- 国内十强少儿编程机构 中国少儿编程机构十强有
- 成人计算机速成培训班 成人计算机速成培训班办
- 孩子学编程网上课程哪家好 儿童学编程比较好的
- 代码编程教学入门软件 代码编程教程