簡體   English   中英

mysql 語法 ERROR 1064 創建觸發器時

[英]mysql syntax ERROR 1064 while creating a trigger

我正在為我的表創建觸發器:- 銀行 Bank(pin, deposit,drawing, balance, accno*, sno) 和 balance Balance(accno*,balance)

我想在插入銀行表后更新余額表中的余額值。 我正在使用 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 服務器版本相對應的手冊,以獲取正確的語法,以便在第 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 ;

我的代碼現在可以工作了謝謝大家

您應該創建與我的代碼相同的觸發器查詢:

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

您的觸發器包含一個語句。 所以它不需要在 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