MySQL `BEFORE INSERT TRIGGER` how can I skip data insertion under condition? [duplicate]

陌路散爱 提交于 2019-12-23 19:22:16

问题


Possible Duplicate:
MySQL Trigger to prevent INSERT under certain conditions

In MySQL BEFORE INSERT TRIGGER how can I skip data insertion under condition?

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:


Two solutions, both raise an error:

  1. Call non-existent stored procedure - CALL non_existent_proc()
  2. Use SIGNAL statement to raise the error (MySQL 5.5).

Example 1:

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

Example 2:

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


来源:https://stackoverflow.com/questions/13511097/mysql-before-insert-trigger-how-can-i-skip-data-insertion-under-condition

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!