How to cancel an INSERT event on Trigger?

Asked

Viewed 454 times

3

Hello, I created a Rigger that every time the user performs a sale is made the debit in the quantity of items, but if I do not take care the quantity can be negative because the user can sell more than they have in the database. So I need a Rigger who doesn’t do the INSERT of that sale if the amount of the sale is greater than available. For that I would need to test before... I started something like this

--Esse é um trigger BEFORE INSERT
    BEGIN
    set @qtd = SELECT itens.quantidade WHERE itens.id = NEW.itens_id;
    IF (NEW.quantidade > @qtd) THEN
        --Cancela o INSERT para que o Trigger do AFTER INSERT n faça a subtração ficando negativa no estoque 
    END IF;

    END

1 answer

1

It is not possible to stop one INSERT for TRIGGER (or at least not without using a gambiarra). You have two options in this case:

  1. Check the quantity before entering the record and display a message to the user;

  2. You can generate an error message using SIGNAL, handle this error in your back-end and display a message to the user¹

¹ Avoid doing such tricks.

  • I would do as you said in example 1.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.