为什么在MySQL触发器查询中出现语法错误?

问题描述 投票:0回答:1

我已经在Oracle SQL中学习了触发器,而在MySQL中却不是那么精通。我试图将触发器命令从Oracle转换为MySQL。首先,我找不到when语句,而是找到了if else。这是我写的:

create trigger overdraft 
after update on account
for each row 
begin
    if account.balance < 0 then
        insert into borrower(select customer_name, account_number from depositor where new.account_number=depositor.account_number);
        insert into loan(select new.account_number, new.branch_name, new.balance);
        update account set balance = 0 where account.account_number = new.account_number;
    end if;
end;

但是我遇到了三个语法错误,首先在左括号的第一个插入语句的末尾说:“语句不完整,期望是:','”。 end的每一个上都有另外两个在说“结束在此位置无效:期望BEGIN,EOF,....”。我的代码有什么问题?我很难弄清楚。

mysql sql triggers
1个回答
0
投票

像这样简单地使用Delimiter

DELIMItER //
create trigger overdraft 
after update on account
for each row 
begin
    if account.balance < 0 then
        insert into borrower(select customer_name, account_number from depositor
         where new.account_number=depositor.account_number);
        insert into loan(select new.account_number, new.branch_name, new.balance);
        update account set balance = 0 
        where account.account_number = new.account_number;
    end if;
end;
DELIMItER ;
© www.soinside.com 2019 - 2024. All rights reserved.