Update Trigger in Mysql

Asked

Viewed 816 times

2

I don’t know much about Mysql and need to make a Trigger to update a line right after its insertion. When the user registers an order he selects how many orders he wants. In the database is saved in one column the type and in another column the quantity he requested. I wish that every time I enter a new request to my Rigger upgrade the type field to (quantity-type) My Rigger is like this but nothing happens after inserting new record:

delimiter $$
create trigger Atualiza_Pedido

after insert on loja.pedido for each row

Begin

if new.method = 'metodo' 

then

update loja.pedido
set tipo = concat(new.quantidade,'-', new.tipo)
where id = new.id;

end if;
END$$

1 answer

0

You don’t have to do the UPDATE, just set the value:

delimiter $$
create trigger Atualiza_Pedido

before insert on loja.pedido for each row
Begin
    if new.method = 'metodo' then
        set new.tipo = concat(new.quantidade,'-', new.tipo);
    end if;
END$$

In fact, this TRIGGER has to be executed before inserting and not after, so I switched the AFTER for BEFORE.

  • I made these changes and made a mistake saying that the type field was not declared. I then added a declare type varchar (30). I made a new request but the field did not update.

  • take off the declare tipo varchar(30) and in place of set tipo place set new.tipo, I’ll change in response too.

  • also didn’t work yet. I tried old but it doesn’t even allow... it’s before Insert even?

Browser other questions tagged

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