MySQL触发IF语法错误
MySQL Trigger IF syntax error
我正在创建一些触发器(使用 MySQL Workbench),并且在其中一个中遇到了关于 if 语句的错误。这是:
CREATE DEFINER = CURRENT_USER TRIGGER `mydatabase`.`transactions_AFTER_UPDATE` AFTER UPDATE ON `transactions` FOR EACH ROW
begin
if (old.idUser != new.idUser) then
signal sqlstate '45000' set message_text = "Can't change transactions' initiator.";
end if;
declare oldAmountTokens integer;
SET @oldAmountTokens = (SELECT tokens from offers WHERE idoffer = old.idOffer);
declare newAmountTokens integer;
SET @newAmountTokens = (SELECT tokens from offers WHERE idoffer = new.idOffer);
declare diff integer;
SET @diff = @newAmountTokens - @oldAmountTokens;
UPDATE users SET tokens = users.tokens + @diff WHERE idUser = new.idUser;
end
MySQL Workbench 告诉我
"missing 'end'" at line 6
('end if;' 所在的位置),
phpMyAdmin 告诉我
"#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 5".
我真的坚持这个,所以如果有人能帮助我,我会很高兴,我在网上搜索了 IF 语法,但我没有发现任何错误...
我觉得你的触发代码没问题,尽管我删除了 transactions
之后的单引号。你有 delimiter
个声明吗?
delimiter //
CREATE DEFINER = CURRENT_USER TRIGGER `mydatabase`.`transactions_AFTER_UPDATE`
AFTER UPDATE ON `transactions` FOR EACH ROW
begin
if (old.idUser <> new.idUser) then
signal sqlstate '45000' set message_text = "Can't change transactions initiator.";
end if;
declare @oldAmountTokens integer;
SET @oldAmountTokens = (SELECT tokens from offers WHERE idoffer = old.idOffer);
declare @newAmountTokens integer;
SET @newAmountTokens = (SELECT tokens from offers WHERE idoffer = new.idOffer);
declare @diff integer;
SET @diff = @newAmountTokens - @oldAmountTokens;
UPDATE users SET tokens = users.tokens + @diff WHERE idUser = new.idUser;
end//
delimiter ;
好的,问题解决了。
首先,我需要使用定界符来避免 end if;
附近的错误。
对于下一个错误,MySQL 要求将 declare
语句放在开始的旁边,感谢这个 post 我找到了答案。谢谢!
我正在创建一些触发器(使用 MySQL Workbench),并且在其中一个中遇到了关于 if 语句的错误。这是:
CREATE DEFINER = CURRENT_USER TRIGGER `mydatabase`.`transactions_AFTER_UPDATE` AFTER UPDATE ON `transactions` FOR EACH ROW
begin
if (old.idUser != new.idUser) then
signal sqlstate '45000' set message_text = "Can't change transactions' initiator.";
end if;
declare oldAmountTokens integer;
SET @oldAmountTokens = (SELECT tokens from offers WHERE idoffer = old.idOffer);
declare newAmountTokens integer;
SET @newAmountTokens = (SELECT tokens from offers WHERE idoffer = new.idOffer);
declare diff integer;
SET @diff = @newAmountTokens - @oldAmountTokens;
UPDATE users SET tokens = users.tokens + @diff WHERE idUser = new.idUser;
end
MySQL Workbench 告诉我
"missing 'end'" at line 6
('end if;' 所在的位置),
phpMyAdmin 告诉我
"#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 5".
我真的坚持这个,所以如果有人能帮助我,我会很高兴,我在网上搜索了 IF 语法,但我没有发现任何错误...
我觉得你的触发代码没问题,尽管我删除了 transactions
之后的单引号。你有 delimiter
个声明吗?
delimiter //
CREATE DEFINER = CURRENT_USER TRIGGER `mydatabase`.`transactions_AFTER_UPDATE`
AFTER UPDATE ON `transactions` FOR EACH ROW
begin
if (old.idUser <> new.idUser) then
signal sqlstate '45000' set message_text = "Can't change transactions initiator.";
end if;
declare @oldAmountTokens integer;
SET @oldAmountTokens = (SELECT tokens from offers WHERE idoffer = old.idOffer);
declare @newAmountTokens integer;
SET @newAmountTokens = (SELECT tokens from offers WHERE idoffer = new.idOffer);
declare @diff integer;
SET @diff = @newAmountTokens - @oldAmountTokens;
UPDATE users SET tokens = users.tokens + @diff WHERE idUser = new.idUser;
end//
delimiter ;
好的,问题解决了。
首先,我需要使用定界符来避免 end if;
附近的错误。
对于下一个错误,MySQL 要求将 declare
语句放在开始的旁边,感谢这个 post 我找到了答案。谢谢!