创建触发器时,mysql语法错误1064

时间:2020-06-01 06:59:09

标签: mysql triggers sql-update create-table

我正在为表格创建触发器:- 银行 银行(pin,存款,取款,余额,accno *,sno) 和 平衡 余额(accno *,余额)

我要在插入银行表后更新余额表中的余额值。 我正在使用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;

但是上面的代码给了我以下错误: 错误1064(42000):您的SQL语法有错误;查看与您的MySQL服务器版本相对应的手册以获取正确的语法,以在'update balance a 设置a.balance =( 当new.withdraw = 1时,则为a.balance-新。”在第3行

3 个答案:

答案 0 :(得分:1)

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 ;

我的代码正在运行 谢谢大家

答案 1 :(得分:0)

您应该创建与我的代码相同的触发器查询:

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;

如果只有两种情况,则应该IF,而不需要Case when

答案 2 :(得分:0)

您的触发器包含一个语句。因此,它不需要在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;