MySQL 儲存過程 游標

2021-08-10 18:44:25 字數 1667 閱讀 4771

儲存過程

-- 本儲存過程有特殊執行迴圈數量的要求,是對security_market_history表進行修正

-- 判斷儲存過程是否存在

drop procedure if exists proc_security_market_history_update;

create procedure proc_security_market_history_update()

begin

declare p_i int default 0; -- 設定初始引數

set @p_days = date_sub(curdate(),interval 0 day); -- 設定需要初始化的值

repeat -- 開啟迴圈

建立儲存過程

create procedure proc_finance_send_charging()

begin

declare cur_time varchar(36);

declare cur_charging_id varchar(10);

declare done int default 0;

/* 宣告游標 */

declare cur cursor for select id from gt_finance_charginginfo where pay_type = 2 and pay_status = 0 

and (type = 2 or type = 3) and create_time <= cur_time and create_time >= 1510122630000;

/* 異常處理 */  

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

set cur_time = unix_timestamp(now())* 1000 - 1000*60*30; 

open cur;

fetch next from cur into cur_charging_id;

repeat

if not done then

update gt_finance_charginginfo set pay_status = 3 , update_time = unix_timestamp(now())*1000 where id = cur_charging_id;

update gt_finance_sendtask set finance_type = 0 ,finance_status = 0, charging_id = null,receive_time = null,

update_time = unix_timestamp(now())*1000,finance_pay_status = if(finance_pay_status = 1,1,0) 

where charging_id = cur_charging_id;

end if;

fetch next from cur into cur_charging_id;

until done end repeat;

close cur;

end執行定時任務

call proc_finance_send_charging()

刪除定時任務

drop procedure proc_finance_send_charging

mysql 游標 儲存過程

1.首先需要注意的是mysql中游標必須要建立在儲存過程中 2.直接上sql 查詢當天資料 select in flow out flow from water meter data where 1 1 and date sys read time curdate 1 order by in flo...

mysql 儲存過程 游標

宣告游標 declare cursor name cursor for select statement 這個語句宣告乙個游標。也可以在子程式中定義多個游標,但是乙個塊中的每乙個游標必須有唯一的名字。開啟游標 open cursor name 這個語句開啟先前宣告的游標。游標fetch fetch ...

MySQL 游標和儲存過程

我們有時候會遇到需要對 從a表查詢的結果集s s 的記錄 進行遍歷並做一些操作 如插入 且這些操作需要的資料或許部分來自s s集合 臨時儲存過程,沒辦法,不能直接在查詢視窗做這些事。drop procedure ifexists proc tmp create procedure proc tmp ...