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

MySQL定时执行存储过程

1,run-->cmd->cd C:\Program Files\MySQL\MySQL Server 5.5\bin
2, mysql -uXXXX -pXXXXXX

3, SHOW FULL PROCESSLIST\G

4,设置sheduler
SET GLOBAL event_scheduler = ON;
SET @@global.event_scheduler = ON;
SET GLOBAL event_scheduler = 1;
SET @@global.event_scheduler = 1;
Similarly, any of these 4 statements can be used to turn off the Event Scheduler:
SET GLOBAL event_scheduler = OFF;
SET @@global.event_scheduler = OFF;
SET GLOBAL event_scheduler = 0;
SET @@global.event_scheduler = 0;

5,create procedure

-- --------------------------------------------
-- Routine DDL
-- Note: comments before and after the routine body will not be stored by the server
-- --------------------------------------------
DELIMITER $$

CREATE DEFINER=`root`@`localhost` PROCEDURE `Get_Info_Every_Day`()
BEGIN
    Declare pIntSumTotalAction int;
    Declare pIntSumNoduedate int;
    Declare pIntSumClosed int;
    Declare pIntSumForinfo int;
    Declare pIntSumOverdue int;
   
    Declare pIntSumTBDin1Week int;
    Declare pIntSumTBDafter1Week int;
    Declare pIntSumPendingJPMO int;
    Declare pIntSumEPS int;
    Declare pIntSumWCI int;
    Declare pIntSumOnTimeClosed int;
    Declare pIntTotal int;  ##统计的时候所有的action items
   
    Declare strStatus varchar(40);
    Declare dDuedate datetime;
    Declare dClosedDate datetime;
    Declare nOverdue int;
    Declare nCountOnTime int;  ##nIsOnTime     count(*)数量  
    declare fetchSeqOk boolean; ## define the flag for loop judgement 

   
   
 
 /*
    Declare my_cursor cursor for select b.status,b.duedate,b.closedate,datediff(now(),b.duedate) as overdue,
    b.fk_actionitem from actionitem a,actionitemdetail b where a.id_actionitem=b.fk_actionitem and a.finishdate=0  
    and status<>'forinfo' and (actionby like '%WEC%' or actionby like '%Consortium%' );
    */
 
    Declare my_cursor cursor for select b.status,b.duedate,b.closedate,datediff(now(),b.duedate) as overdue
    from actionitem a,actionitemdetail b where a.id_actionitem=b.fk_actionitem and a.finishdate=0 
    and status<>'forinfo' and (actionby like '%WEC%' or actionby like '%Consortium%' );


    Declare my_cursor2 cursor for select cast(count(*) as UNSIGNED) as lnOnTimeClosedAI from actionitemdetail
    where datediff(now(),duedate)<=7 and datediff(now(),duedate)>=0
    and (actionby like '%WEC%' or actionby like '%Consortium%' )
    and status='Closed' and datediff(closedate,duedate)<0 order by duedate;
   
   
   
    declare continue handler for not found set fetchSeqOk = true;


   
    set pIntSumTotalAction=0;
    set pIntSumNoduedate=0;
    set pIntSumClosed=0;
    set pIntSumForinfo=0;
    set pIntSumOverdue=0;
    set pIntSumTBDin1Week=0;  
    set pIntSumTBDafter1Week=0;
    set pIntSumPendingJPMO=0;
    set pIntSumEPS=0;
    set pIntSumWCI=0; 
   
 &n