日期:2014-05-16  浏览次数:20920 次

mysql定时任务与存储过程实例
/**
查看event是否开启 : SHOW VARIABLES LIKE '%event_sche%';
将事件计划开启 : SET GLOBAL event_scheduler = 1; 
将事件计划关闭 : SET GLOBAL event_scheduler = 0; 
关闭事件任务 : ALTER EVENT eventName ON COMPLETION PRESERVE DISABLE; 
开启事件任务 : ALTER EVENT eventName ON COMPLETION PRESERVE ENABLE; 
查看事件任务 : SHOW EVENTS ;

delimiter //   设定语句终结符为 //,因存储过程语句用;结束 
**/

DROP TABLE IF EXISTS test;                
CREATE TABLE test (
  id           bigint(11) unsigned NOT NULL AUTO_INCREMENT,
  name         varchar(100) NOT NULL DEFAULT '',
  create_time  datetime,
  PRIMARY KEY (ID)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;


DELIMITER //
DROP PROCEDURE IF EXISTS p_test//
CREATE PROCEDURE p_test()  
BEGIN  
INSERT INTO test(name, create_time) values('testName', now());
END//

DROP EVENT IF EXISTS e_test//
CREATE EVENT e_test  
ON SCHEDULE EVERY 10 second STARTS TIMESTAMP '2014-04-09 01:00:00' 
ON COMPLETION PRESERVE  
DO 
BEGIN   
CALL p_test();
END//


以上事件e_test表示每10秒执行一次p_test()