在 phpMyAdmin 中创建事件时启动事务导致错误
Start Transaction causes an error when creating an event in phpMyAdmin
无论出于何种原因,phpMyAdmin 都不允许我使用“开始事务”创建事件。这是我正在使用的:
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 行
附近使用的正确语法
希望能帮到你。
谢谢。
您需要将 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 ;
无论出于何种原因,phpMyAdmin 都不允许我使用“开始事务”创建事件。这是我正在使用的:
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 行
附近使用的正确语法希望能帮到你。 谢谢。
您需要将 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 ;