我试图找出如果事件失败会发生什么,这是我的事件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表:
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的截图:
更新:尝试将第二个END
从END; $$
更改为END;
,工作台不会显示任何错误,但它也不会运行查询。
更新2 :将行insert ... 'unknown')
更改为insert ... 'unknown');
,在行尾添加;
。该查询现在运行,但我收到另一个错误:Error Code: 1319. Undefined CONDITION: EXCEPTION
答案 0 :(得分:0)
关于END中的错误,仅为
> Your code
> ; //this ; is used to close the insert i think
> END $$
> DELIMITER ;
(不要&#39 ;;' ),试试吧,它现在应该可以使用
答案 1 :(得分:0)
这是正确的查询:
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,因此错误处理是不可能的。 “插入”异常中的错误文本只能从版本5.6开始,参考: