Create Mysql Trigger with input condition

Asked

Viewed 131 times

0

Hello, I’m doing a credit card system and I created a column with the types of card using ENUM('bronze', 'silver', 'gold'), now I want to create a Rigger so that when the user enters with the type, the Rigger automatically sets the limit, and credit conditions in their respective columns. What I’ve tried so far is this:

CREATE DEFINER = CURRENT_USER TRIGGER 
`sistemacc`.`cartaodecredito_AFTER_INSERT` AFTER INSERT ON `cartaodecredito` 
FOR EACH ROW
BEGIN
IF NEW.tipo = 'bronze' THEN 
    SET NEW.limite = 1000 AND NEW.condicoes='até 2x';
ELSEIF NEW.tipo = 'prata' THEN
    SET NEW.limite = 5000 AND NEW.condicoes ='até 8x';
ELSEIF NEW.tipo = 'ouro' THEN
    SET NEW.limite = 10000 AND NEW.condicoes = 'até 12x';
END if;
END

And the error that mysql returns is this :

Error 1362: Updating of NEW Row is not allowed in after Trigger SQL Statement:

  • Switch to Trigger for BEFORE INSERT. Can’t modify the variable in the after pq data has already been entered.

  • I tried the logic of BEFORE INSERT, Trigger is even created, but it doesn’t work, without apparent reason

  • I have never handled mysql triggers. Is the syntax of SET correct? I suspect it should be SET NEW.limite = 1000, NEW.condicoes='até 2x'; (comma instead of AND)

No answers

Browser other questions tagged

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