在phpMyAdmin中创建事件时,启动事务会导致错误

时间:2018-09-29 18:12:27

标签: mysql phpmyadmin transactions mysql-event

无论出于何种原因,phpMyAdmin都不允许我使用Start事务创建事件。这就是我正在使用的:

CREATE EVENT `set_history`
ON SCHEDULE
    EVERY 1 DAY STARTS '2018-9-29 00:00:00'
ON COMPLETION PRESERVE
DISABLE ON SLAVE
DO BEGIN

 START TRANSACTION;


INSERT INTO historical_transactions SELECT * FROM transactions WHERE domain IN (SELECT domain FROM domain JOIN accounts ON domain.accessAbility_ID = accounts.accessAbility_ID WHERE accounts.`renew_at` = DATE(NOW()));

DELETE FROM `transactions` WHERE domain IN (SELECT domain FROM domain JOIN accounts ON domain.accessAbility_ID = accounts.accessAbility_ID WHERE accounts.`renew_at` = DATE(NOW()));

UPDATE `accounts` SET `renew_at`= DATE_ADD(DATE(NOW()), INTERVAL 1 MONTH) WHERE `renew_at` = DATE(NOW());

COMMIT;
END

在事件外运行时,START TRANSACTION,INSERT,DELETE,UPDATE和COMMIT均起作用。这是我不断收到的错误:

#1064-您的SQL语法有误;检查与您的MySQL服务器版本相对应的手册以获取正确的语法,以在第8行的''附近使用

希望您能提供帮助。 谢谢。

1 个答案:

答案 0 :(得分:0)

您需要将DELIMITER定义为($$以外的其他内容(例如:;),最后将其重新定义为(;)。

基本上,PHPMyAdmin解析器会将;解释为执行触发器,并且它将尝试触发查询,而不是将整个事件视为事件的Create语句。因此,重新定义定界符有助于绕过执行。

执行以下操作:

DELIMITER $$
CREATE EVENT `set_history`
ON SCHEDULE
    EVERY 1 DAY STARTS '2018-9-29 00:00:00'
ON COMPLETION PRESERVE
DISABLE ON SLAVE
DO BEGIN

 START TRANSACTION;


INSERT INTO historical_transactions SELECT * FROM transactions WHERE domain IN (SELECT domain FROM domain JOIN accounts ON domain.accessAbility_ID = accounts.accessAbility_ID WHERE accounts.`renew_at` = DATE(NOW()));

DELETE FROM `transactions` WHERE domain IN (SELECT domain FROM domain JOIN accounts ON domain.accessAbility_ID = accounts.accessAbility_ID WHERE accounts.`renew_at` = DATE(NOW()));

UPDATE `accounts` SET `renew_at`= DATE_ADD(DATE(NOW()), INTERVAL 1 MONTH) WHERE `renew_at` = DATE(NOW());

COMMIT;
END $$
DELIMITER ;