程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 數據庫知識 >> MYSQL數據庫 >> MySQL綜合教程 >> mysql 游標嵌套循環實例,mysql游標嵌套實例

mysql 游標嵌套循環實例,mysql游標嵌套實例

編輯:MySQL綜合教程

mysql 游標嵌套循環實例,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

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved