我在创建MySQL(5.1)触发器时遇到了麻烦,我真的没有看到我在这里做错了什么。这是代码:
DELIMITER //
CREATE TRIGGER CheckSoftware BEFORE INSERT ON Software
FOR EACH ROW BEGIN
-- Uppercase the first letter
SET NEW.name = CONCAT(UPPER(SUBSTRING(NEW.name, 1, 1)), SUBSTRING(NEW.name, 2));
-- If price isn't round
IF NEW.price != TRUNCATE(NEW.price) THEN
IF NEW.price - TRUNCATE(NEW.price) <= 0.49 THEN
-- Sets it to xx.49
SET NEW.price = TRUNCATE(NEW.price) + 0.49;
ELSIF (NEW.price - TRUNCATE(NEW.price)) <= 0.99 THEN
-- Sets it to xx.99
SET NEW.price = TRUNCATE(NEW.price) + 0.99;
END IF;
END IF;
END;
//
DELIMITER ;
以及MySQL在我脸上抛出的东西:
ERROR 1064 (42000): 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 ') THEN
IF NEW.price - TRUNCATE(NEW.price) <= 0.49 THEN
' at line 7
非常感谢您的关注!
答案 0 :(得分:1)
DELIMITER //
CREATE TRIGGER CheckSoftware BEFORE INSERT ON Software
FOR EACH ROW BEGIN
-- Uppercase the first letter
SET NEW.name = CONCAT(UPPER(SUBSTRING(NEW.name, 1, 1)), SUBSTRING(NEW.name, 2));
-- If price isn't round
IF NEW.price != TRUNCATE(NEW.price,2) THEN
IF NEW.price - TRUNCATE(NEW.price,2) <= 0.49 THEN
-- Sets it to xx.49
SET NEW.price = TRUNCATE(NEW.price,2) + 0.49;
ELSIF (NEW.price - TRUNCATE(NEW.price,2)) <= 0.99 THEN
-- Sets it to xx.99
SET NEW.price = TRUNCATE(NEW.price,2) + 0.99;
END IF;
END IF;
END;
//
DELIMITER ;
截断函数使用两个参数!
答案 1 :(得分:-1)
DELIMITER #
CREATE TRIGGER CheckSoftware BEFORE INSERT ON Software
FOR EACH ROW BEGIN
SET NEW.name = CONCAT(UPPER(SUBSTRING(NEW.name, 1, 1)), SUBSTRING(NEW.name, 2));
IF NEW.price != TRUNCATE(NEW.price) THEN
...
END IF;
END # -- end of create trigger
DELIMITER ;
编辑 - 差异:
...
END; <----
# -- end of create trigger
DELIMITER ;