我正在寻找一种在MySQL中使用触发器一次验证输入的方法。更具体地说,我想提交一条更新语句,该语句更新2行以上的行(而每个单独的更新将触发触发器,但整个更新语句将不行)。
delimiter //
CREATE TRIGGER checkdistsum
AFTER UPDATE ON tests_detail
for each row
BEGIN
SET @totaldist = (select count(*) from (select sum(td.DISTRIBUTION_PCT) as
total
from models m,.tests t,tests_detail td
where m.model_id = td.MODEL_ID and t.TEST_ID = td.TEST_ID
group by t.test_id having total != 1) as totalquery);
IF @totaldist > 0 THEN signal sqlstate '45000';
END IF;
END;//
触发器将触发查询的值不等于1。但是我希望仅在执行整个update语句后才触发。
更新语句的示例为:
UPDATE tests_detail
SET DISTRIBUTION_PCT = (CASE
WHEN MODEL_ID = 3 THEN .40
WHEN MODEL_ID = 2 THEN .60
ELSE DISTRIBUTION_PCT
END)
WHERE MODEL_ID IN(2,3);
在这种情况下-update语句的总和为1。但是,如果一次仅更新一行,则触发器将过早触发。