2014-12-06 55 views
0

我一直在努力爲MySQL幾個小時的觸發器,我無法弄清楚什麼是錯的。MySQL之前更新觸發器給出錯誤

這裏是我的表結構:

CREATE TABLE IF NOT EXISTS `RentalVideo` (
    `OrderID` int(11) NOT NULL, 
    `VideoBarcode` int(11) NOT NULL, 
    `RentalReturned` tinyint(1) NOT NULL DEFAULT '0', 
    PRIMARY KEY (`OrderID`,`VideoBarcode`), 
    KEY `RentalVideo_VideoBarcode_FK` (`VideoBarcode`) 
) 

下面是一些樣本數據:

INSERT INTO `RentalVideo` (`OrderID`, `VideoBarcode`, `RentalReturned`) VALUES 
(1, 223823, 0), 
(1, 447956, 0), 
(3, 705481, 0), 
(4, 988908, 0), 
(5, 143375, 0); 

這裏有一個不工作的觸發器:

CREATE 
    TRIGGER `RENT_FIVE_VIDEOS_MAX` BEFORE INSERT 
    ON `bollywoo_video`.`RentalVideo` 
FOR EACH ROW BEGIN 
    -- variable declarations 
    DECLARE vRentedVideos int; 
    DECLARE vCustomer int; 
    -- trigger code 

    SELECT RentalOrder.CustID 
    FROM RentalOrder 
    WHERE RentalOrder.OrderID = NEW.OrderID 
    INTO vCustomer; 

    SELECT COUNT(*) 
    FROM RentalOrder, RentalVideo 
    WHERE RentalOrder.CustID = vCustomer 
    AND RentalVideo.RentalReturned = 0 
    AND RentalOrder.OrderId = RentalVideo.VideoID 
    INTO vRentedVideos; 

    IF vRentedVideos >= 5 THEN 
     CALL RAISE_APPLICATION_ERROR(-2000, 'Cannot checkout more than 5 videos'); 
    END IF; 

END 

最後但並非最不重要的,這是我得到的錯誤:

Error 
SQL query: 

CREATE TRIGGER `RENT_FIVE_VIDEOS_MAX` BEFORE INSERT ON `bollywoo_video`.`RentalVideo` 
FOR EACH 
ROW BEGIN -- variable declarations 

DECLARE vRentedVideos INT; 


MySQL said: Documentation 

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '' at line 6 

該錯誤似乎是在之前發生DECLARE vRentedVideos int;

+0

爲什麼使用 '排隊'ON' bollywoo_video'.'RentalVideo'? – HaveNoDisplayName 2014-12-06 00:34:13

+0

這只是我通過查看其他表而嘗試過的東西。我只使用'RentalVideo',但它仍然不起作用,同樣的錯誤。 – mizx 2014-12-06 00:36:51

回答

0

刪除分號,並嘗試這個

DELIMITER $$ 

CREATE 
TRIGGER `RENT_FIVE_VIDEOS_MAX` BEFORE INSERT 
ON `bollywoo_video`.`RentalVideo` 
FOR EACH ROW BEGIN 

... 
END$$ 
DELIMITER ; 
+0

是的,這是完美的作品!我之前嘗試過,但我沒有'DELIMITER';顯然。我會鼓勵你,但是我的代表太低了。謝謝! – mizx 2014-12-06 00:51:19