MySQL在更新TRIGGER之前提供错误

时间:2014-12-06 00:21:17

标签: mysql sql database triggers

我几个小时以来一直在研究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;

之前

1 个答案:

答案 0 :(得分:0)

删除分号并尝试此

DELIMITER $$

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

 ...
END$$
DELIMITER ;