MySQL`BEFORE INSERT TRIGGER`如何在条件下跳过数据插入?

时间:2012-11-22 10:50:10

标签: mysql database triggers

  

可能重复:
  MySQL Trigger to prevent INSERT under certain conditions

在MySQL BEFORE INSERT TRIGGER中如何在条件下跳过数据插入?

delimiter //
drop trigger if exists test_trigger //
create trigger test_trigger before insert on t
for each row
begin
  set @found := false;

  #Some code

  if @found then
    #How to skip the data insertion under condition?
  end if;
end   //

delimiter ;

1 个答案:

答案 0 :(得分:4)

两种解决方案都会引发错误:

  1. 调用不存在的存储过程 - CALL non_existent_proc()
  2. 使用SIGNAL语句引发错误(MySQL 5.5)。
  3. 示例1:

    ...
    IF @found THEN
      CALL non_existent_proc();
    END IF;
    ...
    

    示例2:

    ...
    IF @found THEN
      SIGNAL SQLSTATE '02000' SET MESSAGE_TEXT = 'Wrong data';`
    END IF;
    ...