Error creating trigger with phpmyadmin

1

I have a problem when creating a trigger in phpmyadmin . This is my syntax in the definition field:

DECLARE n_ubicacion VARCHAR(45) DEFAULT '';

IF (OLD.id_ubicacion <> NEW.id_ubicacion) THEN 
SELECT ubicacion INTO n_ubicacion FROM ubicacions WHERE id = NEW.id_ubicacion LIMIT 1;
END IF;

INSERT INTO historico_equipos(usuario, id_equipo, estado, ubicacion, empleado, f_asignacion, created_at) VALUES (CURRENT_USER(), NEW.id, NEW.id_estado, n_ubicacion, NEW.id_empleado, NEW.f_asignacion, CURRENT_DATE())

I can not find where the problem is. In phpmyadmin the following error appears:

Something is wrong in my syntax near:

DECLARE n_ubicacion VARCHAR(45) DEFAULT ''; IF (OLD.id_ubicacion <> NEW.id_ubicacion) THEN
    
asked by Sergio Ramirez Dominguez 07.06.2017 в 23:59
source

1 answer

1

In the end I solved it in the following way:

BEGIN
DECLARE n_ubicacion VARCHAR(45) DEFAULT '';

IF (OLD.id_ubicacion <> NEW.id_ubicacion) THEN 
SELECT ubicacion INTO n_ubicacion FROM ubicacions WHERE id = NEW.id_ubicacion LIMIT 1;
END IF;

INSERT INTO historico_equipos(usuario, id_equipo, estado, ubicacion, empleado, f_asignacion, created_at) 
VALUES (CURRENT_USER(), NEW.id, n_estado, n_ubicacion, n_completo, n_fecha, CURRENT_TIMESTAMP());
END

I added BEGIN and END and made changes with ; .

Thank you.

    
answered by 10.06.2017 в 09:14