0

I am trying to create mysql trigger but getting an error

Error #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 14

Here is my sql script

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 ;

1 Answer 1

2

Remove space between ELSE and IF. It should be ELSEIF as below.

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

Check the demo here

Sign up to request clarification or add additional context in comments.

1 Comment

Oh yes, my bad, actually i have removed those spaces earlier, then later I update sql statement and copied old code from notepad. Thanks btw.. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.