mysql定時任務和儲存過程的小例子

2021-08-21 10:35:19 字數 2264 閱讀 1698

上面說公升級的那裡,是要執行乙個定時任務,每三天執行一次。然後情況如下:

其實重點在於游標的應用和儲存過程的應用(語法挺重要的),貼上**:

delimiter $$

create

/*[definer = ]*/

procedure `test`.`yinliucount_new`()

/*language sql

| [not] deterministic

| | sql security

| comment 'string'*/

begin

declare goodsid varchar(255);

declare keywordgoodsid varchar(255);

declare yinliuc int;

declare done int default 0;

declare cur cursor for select goodsurlid from store_goods;

declare continue handler for not found set done = 1;

open cur;

fetch cur into goodsid;

while done != 1 do

set keywordgoodsid = substring_index(goodsid,"_",-1);

select count(*) into yinliuc from (select * from store_allkeyword where keywordid in(select keywordid from store_goods_keyword where goodsurlid = keywordgoodsid) group by keyword_name)d;

if yinliuc is not null then

update store_goods set yinliu_count = yinliuc where goodsurlid = goodsid;

end if;

fetch cur into goodsid;

end while;

close cur;

end$$

delimiter ;

然後用 新建乙個定時任務,注釋都是一些例子

delimiter $$

-- set global event_scheduler = on$$ -- required for event to execute but not create

create /*[definer = ]*/ event `weanalysis`.`d`

on schedule every 2 day starts '2018-07-13 16:00:00'

on completion preserve

enable

/* uncomment the example below you want to use */

-- scheduleexample 1: run once

-- at 'yyyy-mm-dd hh:mm.ss'/current_timestamp

-- scheduleexample 2: run at intervals forever after creation

-- every 1 [hour|month|week|day|minute|...]

-- scheduleexample 3: specified start time, end time and interval for execution

/*every 1 [hour|month|week|day|minute|...]

starts current_timestamp/'yyyy-mm-dd hh:mm.ss'

ends current_timestamp/'yyyy-mm-dd hh:mm.ss' */

/*[on completion [not] preserve]

[enable | disable]

[comment 'comment']*/

do begin

call test.`yinliucount_new`;

end$$

delimiter ;

然後就設定好了,其他具體的語法,是要去看mysql官方文件還有別人的部落格的,我就不班門弄斧了,這個只是乙個參考的小例子。

mysql 儲存過程 定時任務

建立儲存過程 create procedure if not exists test begin insert into test.test time values curdate end set global event scheduler 0 設定任務器 開關 1開 0關 show variab...

mysql定時任務與儲存過程例項

檢視event是否開啟 show variables like event sche 將事件計畫開啟 set global event scheduler 1 將事件計畫關閉 set global event scheduler 0 關閉事件任務 alter event eventname on c...

MySQL 建立儲存過程及定時任務

建立定時任務名為 event csdn 2019 01 01 00 00 00 起每10s 執行 create event if not exists event csdn on schedule every 10 second starts timestamp 2019 01 01 00 00 0...