Mysql 存儲過程應用

關閉訂單存儲過程sql

beginide

declare done int default 0;fetch

declare orderid varchar(22);spa

#聲明查詢訂單的遊標it

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

#異常處理sed

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

#打開遊標定時任務

open cur;select

#遍歷數據

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;

相關文章
相關標籤/搜索