简体   繁体   English

mysql 语法 ERROR 1064 创建触发器时

[英]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)我正在为我的表创建触发器:- 银行 Bank(pin, deposit,drawing, balance, accno*, sno) 和 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 服务器(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;但上面的代码给了我以下错误:-错误1064(42000) :您的SQL语法有错误; 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检查与您的 MySQL 服务器版本相对应的手册,以获取正确的语法,以便在第 3 行的“更新余额 a set a.balance = (new.withdraw=1 then a.balance - new.) 附近使用正确的语法

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如果你只有两种情况,你应该IF ,不需要Case when

Your trigger contains ONE statement.您的触发器包含一个语句。 So it does NOT need in BEGIN-END block and DELIMITER re-assigning:所以它不需要在 BEGIN-END 块和 DELIMITER 重新分配:

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

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

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