vendredi 3 juillet 2020

Trigger (MYSQL) with if-then-else doesn't works

I have this trigger that doesn't insert or update the "privilegio" and it doesn't give errors in the log

delimiter //

CREATE TRIGGER Privilegio_Pagamento AFTER insert
ON pagamento FOR EACH ROW
BEGIN
    declare numero int(8);
    declare ordutente varchar(64);
    
    select count(*) into numero from pagamento where ordinante = new.ordinante and opera = new.opera;
    select username into ordutente from utente where email = new.ordinante;
    if (numero <> 0) then
        update privilegio P set P.volumi = concat(P.volumi, '-', new.volumi) where opera = new.opera and utente = ordutente;
    else 
        insert into privilegio(utente, opera, volumi) values(ordutente, new.opera, new.volumi);
    end if;
END

The interesting thing is that each single line works alone (the selects, the updates, the insert) so it seems that what fails is the if-then-else, since if I use something like this

delimiter //

CREATE TRIGGER Privilegio_Pagamento AFTER insert
ON pagamento FOR EACH ROW
BEGIN
    declare numero int(8);
    declare ordutente varchar(64);

    select count(*) into numero from pagamento where ordinante = new.ordinante and opera = new.opera;
    select username into ordutente from utente where email = new.ordinante;
    insert into privilegio(utente, opera, volumi) values(ordutente, new.opera, new.volumi);
END

It adds the the "privilegio".

To be sure I also tried this

select if (((select count(*) from pagamento where ordinante = 'abel.ligure73@virgilio.it' and opera = 'Problema Grande e Dama Rossa') <> 0), 'yes', 'no')

and this works. What could be the problem?

Aucun commentaire:

Enregistrer un commentaire