在插入触发器mysql

时间:2016-08-05 21:19:50

标签: mysql triggers

我在一个表上有一对触发器,它将3列连成一个。我需要在不同的格式中将同样的事情做到另一列。 Mysql告诉我,我不能同时拥有相同的触发器。有没有办法将两组触发器组合起来,以便它们分别调用两个东西?我已经找到了这个,似乎无法找到答案或弄明白。只需将SET调用添加到其中就会引发错误。谢谢 如果它们是分开的,那么这是当前的工作触发器。

设置1

CREATE TRIGGER combinedDescNameinsert_trigger
BEFORE INSERT ON  markers
FOR EACH ROW
SET new.combinedDescName = CONCAT(new.descName, '-', new.DoorGroupID, '-', new.DoorID);

CREATE TRIGGER combinedDescNameupdate_trigger
BEFORE UPDATE ON  markers
FOR EACH ROW
SET new.combinedDescName = CONCAT(new.descName, '-', new.DoorGroupID, '-', new.DoorID);

设置2

 CREATE TRIGGER combinedDoorNameinsert_trigger
BEFORE INSERT ON  markers
FOR EACH ROW
SET new.combinedDoorName = CONCAT(new.descName, ' - DoorGroupID: ', new.DoorGroupID, ' - DoorID: ', new.DoorID);

CREATE TRIGGER combinedDoorNameupdate_trigger
BEFORE UPDATE ON  markers
FOR EACH ROW
SET new.combinedDoorName = CONCAT(new.descName, ' - DoorGroupID: ', new.DoorGroupID, ' - DoorID: ', new.DoorID);

1 个答案:

答案 0 :(得分:0)

就像,如果...那么,循环,存储过程等...具有多个语句的触发器必须将它们包含在BEGIN...END中。由于这些语句本身需要终止,因此必须将分隔符暂时更改为主体语句结尾与触发器本身结束之间的差异。

DELIMITER $$

CREATE TRIGGER combinedDescNameinsert_trigger
BEFORE INSERT ON  markers
BEGIN
FOR EACH ROW
   SET new.combinedDescName = CONCAT(new.descName, '-', new.DoorGroupID, '-', new.DoorID);
   SET new.combinedDoorName = CONCAT(new.descName, ' - DoorGroupID: ', new.DoorGroupID, ' - DoorID: ', new.DoorID);
END$$

CREATE TRIGGER combinedDescNameupdate_trigger
BEFORE UPDATE ON  markers
FOR EACH ROW
BEGIN
   SET new.combinedDescName = CONCAT(new.descName, '-', new.DoorGroupID, '-', new.DoorID);
   SET new.combinedDoorName = CONCAT(new.descName, ' - DoorGroupID: ', new.DoorGroupID, ' - DoorID: ', new.DoorID);
END$$

DELIMITER ;