2013-04-11 25 views
0

現在它給我一個1064錯誤在elseif。這是我第一次在5.1中創建觸發器。我不確定我以前使用過的版本,但我成功地使用了該版本。觸發比較登錄名在MySQL 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 ; 

回答

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 
+0

因爲我得到此錯誤#1064 - 您的SQL語法錯誤; ...在DECLARE – user2268697

+0

附近使用正確的語法嗯,你可以嘗試將它向下移動幾行,直到'IF'之前? –

+0

仍然是標籤的1064錯誤權利 – user2268697