【问题标题】:MySQL `BEFORE INSERT TRIGGER` how can I skip data insertion under condition? [duplicate]MySQL`BEFORE INSERT TRIGGER`如何在条件下跳过数据插入? [复制]
【发布时间】:2012-11-22 10:50:10
【问题描述】:

可能重复:
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 ;

【问题讨论】:

    标签: mysql database triggers


    【解决方案1】:

    两种解决方法,都报错:

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

    示例 1:

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

    示例 2:

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

    【讨论】:

    • 有什么办法可以静默做到这一点吗?
    • 使用带参数的过程代替触发器,并在条件后添加记录。
    • 所以没有办法通过触发器?
    • 很遗憾,没有办法。
    • 这很令人失望。但仍然:谢谢:-)
    猜你喜欢
    • 2011-03-16
    • 2017-03-24
    • 2011-09-18
    • 2020-11-08
    • 1970-01-01
    • 1970-01-01
    • 2012-04-12
    • 2012-04-12
    • 1970-01-01
    相关资源
    最近更新 更多