触发两个IF块

时间:2016-11-08 14:34:29

标签: mysql sql triggers

你好我的触发器有问题,当我单独使用它时它可以工作但是没有。如何解决语法错误?

CREATE TRIGGER `C_edit` AFTER UPDATE ON `client`
 FOR EACH ROW 
 IF OLD.name != NEW.name THEN
INSERT INTO `change`(`id_user`, `table`, `column`, `id`, `status`, `before`, `now`) VALUES (OLD.id_user_edit, 'client', 'name', OLD.id_client, '4', OLD.name, NEW.name);
END IF;
IF OLD.name_sql != NEW.name_sql THEN
INSERT INTO `change`(`id_user`, `table`, `column`, `id`, `status`, `before`, `now`) VALUES (OLD.id_user_edit, 'client', 'name_sql', OLD.id_client, '4', OLD.name_sql, NEW.name_sql);
END IF;
error: #1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 4 

2 个答案:

答案 0 :(得分:0)

由于您在触发器体内使用了多个语句,因此需要使用MySQL的begin ... end compound statement syntax以及<a asp-controller="Foo" asp-action="Edit" asp-route-id="{{myAngularControllerInstance.selectedFooId}}">Edit</a> 语句来更改分隔符:

delimiter

答案 1 :(得分:0)

尝试使用如下的BEGIN / END块:

DELIMITER $$
CREATE TRIGGER `C_edit` AFTER UPDATE ON `client`
  FOR EACH ROW 
BEGIN
 IF OLD.name != NEW.name THEN
   INSERT INTO `change`(`id_user`, `table`, `column`, `id`, `status`, `before`, `now`) VALUES (OLD.id_user_edit, 'client', 'name', OLD.id_client, '4', OLD.name, NEW.name);
END IF;
IF OLD.name_sql != NEW.name_sql THEN
  INSERT INTO `change`(`id_user`, `table`, `column`, `id`, `status`, `before`, `now`) VALUES (OLD.id_user_edit, 'client', 'name_sql', OLD.id_client, '4', OLD.name_sql, NEW.name_sql);
END IF;
END  $$
DELIMITER;

或没有DELIMITER

CREATE TRIGGER `C_edit` AFTER UPDATE ON `client`
  FOR EACH ROW 
BEGIN
 IF OLD.name != NEW.name THEN
   INSERT INTO `change`(`id_user`, `table`, `column`, `id`, `status`, `before`, `now`) VALUES (OLD.id_user_edit, 'client', 'name', OLD.id_client, '4', OLD.name, NEW.name);
END IF;
IF OLD.name_sql != NEW.name_sql THEN
  INSERT INTO `change`(`id_user`, `table`, `column`, `id`, `status`, `before`, `now`) VALUES (OLD.id_user_edit, 'client', 'name_sql', OLD.id_client, '4', OLD.name_sql, NEW.name_sql);
END IF;
END

PHPMyAdmin不需要DELIMITER,而MySQL Workbench和其他人则需要。