Mysql trigger does not identify fields

Asked

Viewed 13 times

1

good night,

I have a Trigger in Mysql, but it doesn’t work, it just calls a precedent by passing a value. It doesn’t generate an error, but it doesn’t work.

Trigger code:

delimiter $$
create trigger trg_hardware_ai after insert on hardware
for each row
begin
    #call criar_setores(new.id);
    insert into setor (nome, defasados, atualizados, total) values ('ALAN01', 0, 1, 1);
end $$
delimiter;

code of the trial:

delimiter $$
CREATE PROCEDURE criar_setores(id_hardware int)
BEGIN
    declare nome_setor varchar(50);
    declare defasados_setor, atualizados_setor, memoria, existe_setor int;
    
    select name, memory into nome_setor, memoria from hardware where id = id_hardware;
    select substring(nome_setor, 1, 6) into nome_setor;
    
    if ((memoria / 4000) >= 1) then
        set defasados_setor = 0;
        set atualizados_setor = 1;
    else
        set defasados_setor = 1;
        set atualizados_setor = 0;
    end if;
    
    select id into existe_setor from setor where nome = nome_setor;

    if (existe_setor is not null) then
        update setor set 
            defasados = defasados + defasados_setor, 
            atualizados = atualizados + atualizados_setor,
            total = total + 1
            where nome = nome_setor;
    else    
        insert into setor (nome, defasados, atualizados, total) 
            values (nome_setor, defasados_setor, atualizados_setor, 1);
    end if;
END $$
DELIMITER;

if I call the trial directly it works:

call criar_setores(14);

It’s like Trigger didn’t recognize the new.id field

  • tried to insert directly into a table to test Trigger?

No answers

Browser other questions tagged

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