MySQLでこのトリガーがあります
DROP TRIGGER IF EXISTS BeforeGivingRight;
DELIMITER $$
CREATE TRIGGER BeforeGivingRight
BEFORE INSERT ON right_code
FOR EACH ROW
BEGIN
DECLARE num_rows INT NULL;
SELECT count(right_code) FROM valid_rights WHERE right_code =
new.user_right.right INTO num_rows;
IF num_rows = 0
THEN
set msg = concat('Error: That right is not allowed!',
cast(new.user_right.right as char));
signal sqlstate '45000' set message_text = msg;
END IF;
END $$
DELIMITER ;
そしてそれは私にこのエラーを与えます
#1064 - 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 'NULL; SELECT
count(right_code) FROM valid_rights WHERE right_code = new.user_' at line 5
ここで何が問題になっていますか?私がやりたいのは、権利(たとえば、201)を挿入する前に、valid_rights
トリガーを使用して、それが存在するかどうかを確認することです。
編集(回答)
したがって、例外をスローするトリガーが必要な場合は、次を使用します。
DROP TRIGGER IF EXISTS BeforeGivingRight;
DELIMITER $$
CREATE TRIGGER BeforeGivingRight
BEFORE INSERT ON user_rights
FOR EACH ROW
BEGIN
DECLARE num_rows INT DEFAULT NULL;
DECLARE msg VARCHAR(32) DEFAULT "";
SELECT count(valid_rights.right_code) FROM valid_rights WHERE right_code = new.right INTO num_rows;
IF num_rows = 0
THEN
set msg = concat('Error: That right is not allowed!', cast(new.right as char));
signal sqlstate '45000' set message_text = msg;
END IF;
END $$
DELIMITER ;