我是MySql触发器的新手。我有一个创建触发器来检查插入值的任务,但是它在if语句中引发错误,我不知道如何解决
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'IF new.price > @maxi
THEN
ROLLBACK
else
INSERT INTO Laptop
SELECT * FROM inserted
at line 1
代码:
CREATE TRIGGER ChekInsert BEFORE INSERT ON Laptop FOR EACH ROW
BEGIN
SELECT @maxi := MAX(price)*0.5 FROM Laptop
IF new.price > @maxi
THEN
ROLLBACK
else
INSERT INTO Laptop
SELECT * FROM inserted
end if
END
答案 0 :(得分:0)
更改了我的代码,一切正常
delimiter $$
CREATE TRIGGER ChekInsert BEFORE INSERT ON Laptop FOR EACH ROW
BEGIN
declare maxi float;
select MAX(price)*0.5 into maxi FROM Laptop;
if NEW.price > @maxi then
SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'price is incorrect';
end if;
end $$
delimiter;
要在触发器中包含多个语句,您必须更改默认的定界符,并且不需要回滚,因为它会在引发错误时触发