我想创建一个触发器,该触发器在特定表上的插入之后有2个条件,如果第一个条件为true,则执行插入;如果第二个条件为true,则进行更新。
类似的东西
create trigger nameoftrigger
after insert on a
for each row
condition regarding newrowattribute and a.sameattribute
insert into a ---
condition regarding newrowattribute and a.sameattribute
update a different table with same attributes
但是我不知道执行此操作的正确语法,即检查2个不同的条件并在第一个条件中执行插入操作,并在第二个条件中执行更新操作。
答案 0 :(得分:0)
MySQL不支持Microsoft SQL Server之类的“ INSTEAD OF”触发器。
INSERT触发器可以执行插入操作,否则将中止并且不执行任何操作。
您应该在应用程序代码中处理条件。
Raymond Nijland提醒我,您也不能编写对执行该触发器的表执行任何INSERT或UPDATE的触发器。好了,您可以创建这样的触发器,但是执行时会产生错误。
mysql> create trigger nameoftrigger after insert on a for each row
insert into a values (1);
Query OK, 0 rows affected (0.05 sec)
mysql> insert into a values (2);
ERROR 1442 (HY000): Can't update table 'a' in stored function/trigger because
it is already used by statement which invoked this stored function/trigger.
答案 1 :(得分:0)
有关控制流程,请参见IF ... THEN ... ELSE ... END IF
。
如果您要修改要插入的内容,请执行
IF (...)
THEN
SET NEW.col1 = <<some expression>>;
END IF
然后只需让INSERT
发生。
如果IF表达式或col1的表达式需要查看INSERT
对于col1的值,则将其称为OLD.col1
。