Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 ;
like image 597
sdespont Avatar asked Oct 18 '25 01:10

sdespont


1 Answers

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;
...
like image 86
Devart Avatar answered Oct 20 '25 16:10

Devart



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!