简体   繁体   English

创建mysql触发器时出现1064错误

[英]Getting 1064 error while creating mysql trigger

I am trying to create mysql trigger but getting an error 我正在尝试创建mysql触发器,但出现错误

Error #1064 - You have an error in your SQL syntax; 错误#1064-您的SQL语法有错误; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 14 检查与您的MySQL服务器版本相对应的手册,以找到在第14行的''附近使用的正确语法

Here is my sql script 这是我的SQL脚本

delimiter $$
CREATE TRIGGER `update_account_balance_after_transfer` AFTER INSERT ON `balance_transfer`
 FOR EACH ROW  
    BEGIN
    IF(NEW.from_account_type='CUSTOMER') THEN
        UPDATE customer c SET c.balance = c.balance-NEW.amount WHERE c.customer_id= NEW.from_account_id;
    ELSE IF(NEW.from_account_type='Vendor') THEN
        UPDATE vendor V SET v.balance = v.balance-NEW.amount WHERE v.vendor= NEW.from_account_id;
    END IF;
    IF(NEW.to_account_type='VENDOR') THEN
        UPDATE customer c SET c.balance = c.balance+NEW.amount WHERE c.customer_id= NEW.to_account_id;
    ELSE IF(NEW.to_account_type='Vendor') THEN
        UPDATE vendor V SET v.balance = v.balance+NEW.amount WHERE v.vendor= NEW.to_account_id;
    END IF;
END $$
delimiter ;

Remove space between ELSE and IF . 删除ELSEIF之间的空间。 It should be ELSEIF as below. 它应为ELSEIF ,如下所示。

delimiter $$
CREATE TRIGGER `update_account_balance_after_transfer` AFTER INSERT ON `balance_transfer`
 FOR EACH ROW  
    BEGIN
    IF(NEW.from_account_type='CUSTOMER') THEN
        UPDATE customer c SET c.balance = c.balance-NEW.amount WHERE c.customer_id= NEW.from_account_id;
    ELSEIF(NEW.from_account_type='Vendor') THEN
        UPDATE vendor V SET v.balance = v.balance-NEW.amount WHERE v.vendor= NEW.from_account_id;
    END IF;
    IF(NEW.to_account_type='VENDOR') THEN
        UPDATE customer c SET c.balance = c.balance+NEW.amount WHERE c.customer_id= NEW.to_account_id;
    ELSEIF(NEW.to_account_type='Vendor') THEN
        UPDATE vendor V SET v.balance = v.balance+NEW.amount WHERE v.vendor= NEW.to_account_id;
    END IF;
END $$
delimiter ;

MySQL IF Syntax MySQL IF语法

Check the demo here 此处查看演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM