Mysql 存儲過程應用

關閉訂單存儲過程

begin

declare done int default 0;

declare orderid varchar(22);

#聲明查詢訂單的遊標

declare cur cursor for select orders.order_id from orders where orders.order_state =  'wait_pay';

#異常處理

declare continue handler for sqlstate '02000' set done = 1;

#打開遊標

open cur;

#遍歷數據

fetch next from cur into orderid;

repeat

if not done then

#根據訂單編號查詢訂單的下單時間

select orders.order_xiatime into @a from orders where order_id=orderid;

#根據訂單下單時間獲取1天后的時間(先用一天,存儲過程正常使用後改爲12小時間)

SELECT ADDDATE(@a, INTERVAL 24 Hour) into @b;

#判斷會員在1天內是否未做任何處理

if @b<now() then

#自動更新訂單的狀態

update orders set order_state='trade_colsed',order_closetime = now() ,close_order_reason='訂單關閉時間到,定時任務正常關閉' where order_id=orderid;

#若有多個操作可以在這裏接着些

#回收庫存(調用另一個存儲過程)

call recoveryOfInventory(orderid);

end if;

end if;

fetch next from cur into orderid;

until done end repeat;

close cur;


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章