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 ;
解决方案
Two solutions, both raise an error:
Call non-existent stored procedure - CALL non_existent_proc()
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;
...