mysql 游标嵌套循环实例

BEGIN
#Routine body goes here...
####所有的2个小时之前生成的待支付订单更新为已过期
DECLARE tmp_id INT;
DECLARE tmp_order_id VARCHAR(22);
DECLARE flag INT;
DECLARE update_cursor CURSOR FOR SELECT id, order_id FROM yjl_item_order WHERE order_status = 0 AND add_time < DATE_SUB(NOW(),INTERVAL 2 HOUR);
DECLARE CONTINUE HANDLER FOR NOT FOUND SET flag=1;
SET flag=0;
OPEN update_cursor;
loop_xx1: LOOP /*循环*/
FETCH update_cursor INTO tmp_id,tmp_order_id;
IF flag=1 THEN
leave loop_xx1;
END IF;
BEGIN
DECLARE tmp_price_id INT;
DECLARE tmp_num INT;
DECLARE flag1 INT;
DECLARE cursor2 CURSOR FOR SELECT price_id,code_num FROM yjl_order_info WHERE order_id = tmp_order_id;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET flag1=1;
###更新此订单的抵价券和邀请码
UPDATE log_member_voucher a LEFT JOIN yjl_member_voucher b ON a.mixed_id = b.id SET a.status = 3, b.voucher_status = 0 WHERE a.order_id =

tmp_order_id AND a.status = 0;
UPDATE yjl_item_order SET order_status = 2 WHERE id = tmp_id;

SET flag1=0;
OPEN cursor2;
loop_xx2: LOOP
FETCH cursor2 INTO tmp_price_id, tmp_num;
IF flag1=1 THEN
leave loop_xx2;
END IF;
###库存回滚
UPDATE yjl_item_store SET realse_num = realse_num + tmp_num WHERE price_id = tmp_price_id;
END LOOP;
END;
/*update set where*/
END LOOP;
CLOSE update_cursor ;
END

上一篇:codeforce-191E-Thwarting Demonstrations(树状数组+二分+离散)


下一篇:axel源码学习(0)——程序逻辑