4

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 ;
Community
  • 1
  • 1
sdespont
  • 13,915
  • 9
  • 56
  • 97

1 Answers1

5

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;
...
Devart
  • 119,203
  • 23
  • 166
  • 186