检索失败事件的错误

Retrieving error for failed event

我想知道如果事件失败会发生什么,这是我的事件 sql:

DELIMITER $$

CREATE EVENT IF NOT EXISTS `cdr2015_daily_update`
ON SCHEDULE EVERY 1 DAY
    STARTS (TIMESTAMP(CURRENT_DATE) + INTERVAL 1 DAY + INTERVAL 3 HOUR)
DO 
BEGIN
DECLARE EXIT HANDLER FOR MYSQLEXCEPTION, MYSQLWARNING
    BEGIN
    insert into events_state values ('cdr2015_daily_update', 'true', now(), 'unknown', 'unknown')
    END;

    insert into 
        cdr2015_v2 (clid, src, dst, dcontext, channel, dstchannel) 
    select 
        calldate, clid, src, dst, dcontext, channel, dstchannel
        from cdr where DATE_FORMAT(calldate, '%Y-%m-%d') = subdate(current_date, 1); -- yesterday calls 
        
END; $$

DELIMITER ;

每天在 03:00 备份前一天的通话。此事件肯定会失败,但我想知道错误,例如:

`Error Code: 1136. Column count doesn't match value count at row 1` , is it possible to catch this error and insert it into another table? 

这是events_state table:

CREATE TABLE `events_state` (
`event` varchar(255) DEFAULT NULL,
`failed` varchar(255) DEFAULT NULL,
`fail_date` datetime,
`reason1` varchar(255) DEFAULT NULL,
`reason2` varchar(255) DEFAULT NULL,
`RID` int(11) NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`RID`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1

现在事件正在插入这个:

insert into events_state values ('cdr2015_daily_update', 'true', now(), 'unknown', 'unknown')

可以改成这样吗?

insert into events_state values ('cdr2015_daily_update', 'true', now(), MYSQLEXCEPTION, MYSQLWARNING) 

我也无法 运行 这个事件,因为我在两个 END; 上都遇到了错误,这里是 mysql-workbench 的一些截图:

更新: 尝试将第二个 ENDEND; $$ 更改为 END; ,workbench 不会显示任何错误,但它不会运行查询任一个。

更新 2: 将行 insert ... 'unknown') 更改为 insert ... 'unknown'); ,在行尾添加 ; 。查询现在 运行s,但我收到另一个错误:Error Code: 1319. Undefined CONDITION: EXCEPTION

关于END中的错误,只是

> Your code
> ; //this ; is used to close the insert i think
> END $$
> DELIMITER ;

(不要放';'),试试看,现在应该可以了

这是正确的查询:

SET GLOBAL event_scheduler = ON;

DELIMITER $$

CREATE EVENT IF NOT EXISTS `cdr2015_daily_update`
ON SCHEDULE EVERY 1 DAY
    STARTS (TIMESTAMP(CURRENT_DATE) + INTERVAL 1 DAY + INTERVAL 3 HOUR)
DO 
BEGIN
    DECLARE CONTINUE HANDLER FOR SQLEXCEPTION
    BEGIN
    insert into events_state values ('cdr2015_daily_update', 'true', now(), 'unknown', 'unknown', 0);
    END;

    insert into 
        cdr2015 (calldate, clid, src, dst, dcontext, channel, dstchannel) 
    select 
        calldate, clid, src, dst, dcontext, channel, dstchannel
        from cdr where DATE_FORMAT(calldate, '%Y-%m-%d') = subdate(current_date, 1)
        limit 10;

END; $$
DELIMITER ;

我的 MySQL 版本是 5.1,因此无法进行错误处理。 "Inserting" 异常的错误文本只能从 5.6 版本开始,参考:

Getting SQLEXCEPTION message in procedures MySQL 5.5.x

MySQL Stored Procedure Error Handling