Search code examples
mysqltriggerssql-updatecreate-table

mysql syntax ERROR 1064 while creating a trigger


I am creating a trigger for my tables:- bank Bank(pin, deposit, withdraw, balance, accno*, sno) and balance Balance(accno*,balance)

I want to update the value of balance in my balance table after insertion in the bank table. I am using a MySQL server (wamp64 mysql8.0.18)

mysql> create trigger update_account
    -> after insert on bank
    -> begin
    -> update balance as a
    -> set a.balance=(case
    -> when new.withdraw=1 then a.balance-new.withdraw
    -> else a.balance+new.withdraw
    -> end)
    -> where a.accno = new.accno;

but the above code gives me the following error:- ERROR 1064 (42000): 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 'update balance a set a.balance = (case when new.withdraw=1 then a.balance - new.' at line 3


Solution

  • DELIMITER $$
    DROP PROCEDURE my_procedure$$
    create trigger update_account
    after insert
    on bank
    for each row
    begin
    update balance as a
    set a.balance= a.balance + new.withdraw + new.deposit
    where a.accno=new.accno;
    END$$
    DELIMITER ;
    

    My code is now working thank you all