我正在尝试设置一个触发器(从单独的.sql文件运行)以防止像这样插入:
create trigger insert_formulas before insert on formulas
for each row
if ((new.formula_id_1 is not null) and (new.semantic_id_1 is not null))
or ((new.formula_id_2 is not null) and (new.semantic_id_2 is not null))
then
signal sqlstate '45100';
set @message_text = 'A formula cannot be a definition at the same time';
end if;
它在第6行附近产生错误1064,在第1行附近产生错误1064.我做错了什么?
提前致谢。
答案 0 :(得分:0)
9.4. User-Defined Variables @message_text
与13.6.7.5. SIGNAL Syntax定义中的MESSAGE_TEXT
不同吗?
以下代码正确创建了触发器:
DELIMITER $$
CREATE TRIGGER `insert_formulas` BEFORE INSERT ON `formulas`
FOR EACH ROW
BEGIN
IF((NEW.`formula_id_1` IS NOT NULL AND NEW.`semantic_id_1` IS NOT NULL) OR
(NEW.`formula_id_2` IS NOT NULL AND NEW.`semantic_id_2` IS NOT NULL)) THEN
SIGNAL SQLSTATE '45100'
SET MESSAGE_TEXT = 'A formula cannot be a definition at the same time';
END IF;
END$$
DELIMITER ;
此处SQL Fiddle。