0

Dummy question: Have the next code in MySQL, but when run it in HeidiSQL, show a sintaxis error near '// CREATE TRIGGER'. I'm not sure if that happen because the trigger doesn't exists. This is the code:

DROP TRIGGER IF EXISTS oportunidades_movimiento_entregado //
CREATE TRIGGER oportunidades_movimiento_entregado;
AFTER INSERT ON historial_entregado
FOR EACH ROW BEGIN
UPDATE oportunidades
SET oportunidades.fechaModificado = NEW.fecha_creacion
WHERE NEW.oportunidad_id = oportunidades.id;
END//

UPDATE

DELIMITER $$
DROP TRIGGER IF EXISTS oportunidades_movimiento_entregado $$
CREATE TRIGGER oportunidades_movimiento_entregado
AFTER INSERT ON historial_entregado
FOR EACH ROW BEGIN
UPDATE oportunidades
SET oportunidades.fechaModificado = NEW.fecha_creacion
WHERE NEW.oportunidad_id = oportunidades.id;
END; $$
DELIMITER //

This works, but now my doubt is... All the code (the DROP and the CREATE) will run everytime, or just the CREATE? I need this because I'm working with 2 tables, in one of them I insert/update the data, after with a trigger insert some of the columns in another table to keep a historial, but when I make a INSERT or UPDATE query in the first table, MySQL display the error #1442

Can't update table oportunidades in stored/function trigger because it's already used by statement which invoked this stored function/trigger.

4

1 回答 1

0

要使其正常工作,请先设置分隔符并在 CREATE TRIGGER 语句之后将其重置。删除 CREATE TRIGGER 行末尾的分号。

这应该有效:

DELIMITER //
DROP TRIGGER IF EXISTS oportunidades_movimiento_entregado //

CREATE TRIGGER oportunidades_movimiento_entregado    -- no trailing semicolon here
    AFTER INSERT ON historial_entregado
    FOR EACH ROW BEGIN
        UPDATE oportunidades
        SET oportunidades.fechaModificado = NEW.fecha_creacion
        WHERE NEW.oportunidad_id = oportunidades.id;
    END//

DELIMITER ;
于 2014-08-18T17:34:42.357 回答