为什么触发器上的“else”被认为是错误的?

1mrurvl1  于 2021-06-17  发布在  Mysql
关注(0)|答案(2)|浏览(383)

我写了以下代码

DELIMITER $$

CREATE TRIGGER bm_keuangan 
AFTER UPDATE ON pembayaran_project FOR EACH ROW
BEGIN
DECLARE nama_project varchar(100);
DECLARE cek_di_keuangan varchar(100);
IF (!(isnull(NEW.deleted_at))) THEN
    UPDATE keuangan SET keuangan.deleted_at = now() WHERE keuangan.ref = NEW.id AND keuangan.jk_id = 5;
ELSE

END IF;

END $$
DELIMITER ;

我犯了个错误:

1064-您的sql语法有错误;检查与您的mariadb服务器版本相对应的手册,以获得使用near'end if的正确语法;

在第10行结束

o0lyfsai

o0lyfsai1#

来自文档(我的):

IF search_condition THEN statement_list
    [ELSEIF search_condition THEN statement_list] ...
    [ELSE statement_list]
END IF

每个 statement_list 由一个或多个sql语句组成;空的 statement_list 不允许。

a64a0gku

a64a0gku2#

else块为空,这在mysql中是不允许的。您应该从代码中删除关键字else,如下所示:

DELIMITER $$

CREATE TRIGGER bm_keuangan 
AFTER UPDATE ON pembayaran_project FOR EACH ROW
BEGIN
DECLARE nama_project varchar(100);
DECLARE cek_di_keuangan varchar(100);
IF (!(isnull(NEW.deleted_at))) THEN
    UPDATE keuangan SET keuangan.deleted_at = now() WHERE keuangan.ref = NEW.id AND keuangan.jk_id = 5;

END IF;

END $$
DELIMITER ;

相关问题