简体   繁体   中英

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

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

You should create trigger query same my code:

create trigger update_account
    after insert
    on bank
    for each row
begin
    update balance as a
    set a.balance=(IF(new.withdraw = 1, a.balance - new.withdraw, a.balance + new.withdraw))
    where a.accno = new.accno;
END;

And if you have only two cases, you should IF , not need Case when

Your trigger contains ONE statement. So it does NOT need in BEGIN-END block and DELIMITER re-assigning:

CREATE TRIGGER update_account
AFTER INSERT
ON bank
FOR EACH ROW
UPDATE balance
SET balance = balance + new.withdraw + new.deposit
where accno=new.accno;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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