0

I want to create a trigger that delete any new row I insert if the value is more than a number, for example 8.

I tried this:

DELIMITER |
CREATE TRIGGER mytrigger AFTER INSERT ON mytab
    FOR EACH ROW 
    BEGIN
        DELETE FROM mytab WHERE myparameter > 8;
    END
|

DELIMITER ;

I tried to:

INSERT INTO mytab VALUES (9);

and this is the message error:

#1442 - Can't update table 'mytab' in stored function/trigger because it is already used by statement which invoked this stored function/trigger. 

also if I put:

INSERT INTO mytab VALUES (4);

it runs the same error...

Thanks!

2
  • you can not do insert/update/delete on the same table where you are running the trigger. Commented Dec 2, 2014 at 17:47
  • What is wrong? You need to give more info: the error message would be a good start. Commented Dec 2, 2014 at 17:47

1 Answer 1

2

Don't delete! Just produce an error on the insert:

DELIMITER |
CREATE TRIGGER mytrigger BEFORE INSERT ON mytab
    FOR EACH ROW 
    BEGIN
        IF new.myparameter > 8 THEN
             signal sqlstate '45000' set message_text = 'Parameter value can be no larger than 8';
        END IF;
    END
|

DELIMITER ;
Sign up to request clarification or add additional context in comments.

1 Comment

You haven't erase the line of delete :P Thanks a lot Gordon!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.