查看当前数据库有什么定时任务
SELECT * FROM mysql.event
1、查询定时任务是否打开
show variables like '%event_sche%';
2、如果查询的Value值是OFF,需要设置为ON,才可以创建定时任务
set global event_scheduler=1;
否则会报一下问题
3、创建定时任务
CREATE EVENT `AddDataToMuserTable`
ON SCHEDULE EVERY 1 DAY STARTS '2019-11-13 05:00:00'
ON COMPLETION NOT PRESERVE
ENABLE
DO
begin
declare c int;
set c = (SELECT COUNT(*) from m_user_dept WHERE `user_code` ='xxx' AND dept_code!='xx');
if c <= 1 then
insert into m_user_dept(user_code,dept_code) values('xxx' ,'xx');
end if;
end;
4、如果事情不起作用了,要检查是否开启了定时任务。
查询事件任务
SHOW EVENTS ;
将事件计划任务开启:
ALTER
EVENT eventName
ON
COMPLETION PRESERVE ENABLE;
将事件计划任务关闭
ALTER
EVENT eventName
ON
COMPLETION PRESERVE DISABLE;