0

现在它给我一个 1064 错误elseif。这是我第一次在 5.1 中创建触发器。我不确定我以前使用的版本,但我成功使用了那个版本。

DROP TRIGGER IF EXISTS greentrucks.iCustomer;
DELIMITER GO
CREATE TRIGGER greentrucks.iCustomer AFTER INSERT
ON customer 
For each ROW BEGIN 

DECLARE count int;
SET @count = FOUND_ROWS();
If @count = 0
THEN LEAVE

ELSEIF EXISTS ( SELECT *
            FROM INSERTED i
            WHERE  EXISTS (
            SELECT p.email
            FROM greentrucks.customer p
            WHERE i.email = p.email))

THEN BEGIN
    RAISERROR('That Email is already in use!',16,1);
    IF @@Trancount >0
        ROlLBACK TRANSACTION;
END IF
END IF
END
GO
DELIMITER ;
4

1 回答 1

0

LEAVE需要一个标签。请参阅LEAVE 文档LOOP 文档中的示例。此外,您需要LEAVE用分号结束您的语句。

MySQL 似乎没有关于LEAVE触发器的任何官方示例。这篇博文将标签放在BEGIN. 基于此,尝试这样的事情:

DROP TRIGGER IF EXISTS greentrucks.iCustomer;
DELIMITER GO
CREATE TRIGGER greentrucks.iCustomer AFTER INSERT
ON customer 
For each ROW
my_label: BEGIN

DECLARE count int;
SET @count = FOUND_ROWS();
If @count = 0
THEN LEAVE my_label;

... and the rest of your code
于 2013-04-11T04:04:13.233 回答