用于比较mysql 5.1中的登录名的触发器

时间:2013-04-11 03:48:40

标签: mysql validation triggers mysql-5.1

现在它在elseif给我1064错误。这是我第一次在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 ;

1 个答案:

答案 0 :(得分:0)

LEAVE需要一个标签。请参阅LEAVE documentationsample in the LOOP documentation。此外,您需要使用分号终止LEAVE语句。

MySQL在触发器中似乎没有LEAVE的任何官方示例。 This blog post将标签放在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