Delete on an UPDATE Trigger

Asked

Viewed 557 times

-1

How can I delete a record that is being entered in a specific condition in a Rigger?

example:

USE `scompraslenovo`;
DELIMITER $$
CREATE TRIGGER `precad_fornecedor_BUPD` BEFORE UPDATE ON `precad_fornecedor` FOR EACH ROW
BEGIN
    IF (new.f1 IS NULL) AND (new.f2 IS NULL) AND (new.f3 IS NULL) THEN
        DELETE FROM precad_fornecedor
        WHERE precad_fornecedor.codigo = NEW.codigo;
    END IF;
END

Is there any way to make it work?

  • This code only executes when you update a record, not when you enter it.

  • 1

    Putting out the Trigger?

  • Deleting Trigger will only cause the action it performs not to occur.

  • 2

    Exactly, if Trigger is entering a record, and he wants to delete that record, just don’t insert it, that is, don’t perform the insertion action, or delete Trigger

  • They could edit the code to see how it would look in this case?

1 answer

3

"Deleting a record that is being inserted" does not make much sense. If you will delete the record that you will insert, it is more practical not to insert it right? So in case you would like to prevent that particular record, if it does not meet the conditions, is not entered right? In this case, just you exchange update by Insert and put the conditions.

USE `scompraslenovo`;
DELIMITER $$
CREATE TRIGGER `precad_fornecedor_BUPD` before insert ON `precad_fornecedor` FOR EACH ROW
BEGIN
IF ...
Where... 
END IF;
END

before inserting, Trigger will search the conditions. If you do not answer, the record will not be inserted. Avoiding insertion and then deleting.

Browser other questions tagged

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