触发器内有多个插入/更新语句?

Multiple insert/update statements inside trigger?(触发器内有多个插入/更新语句?)
本文介绍了触发器内有多个插入/更新语句?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只是一个简单的问题,毫无疑问有人会知道答案.

Just a quick question that no doubt someone out there will know the answer to.

我需要能够在触发器中进行多次插入/更新.每次尝试都以失败告终:(

I need to be able to do multiple insert/updates within a trigger. Every attempt ends with failure :(

DROP TRIGGER IF EXISTS `Insert_Article`//
CREATE TRIGGER `Insert_Article` AFTER INSERT ON `Article`
 FOR EACH ROW insert into FullTextStore (`Table`, `PrimaryKey`, `ColumnName`, `Data`, `Created`) values ('Article', NEW.ArticleID, 'Description', NEW.Description, UNIX_TIMESTAMP())
//

目前,上面只是在父表插入时向表中插入一行.这工作正常.

At the moment, the above simply inserts a row into a table when the parent table inserts. This works fine.

为了让它与多个值一起工作,我需要做

To get this to work with mulitple values I need to do

DROP TRIGGER IF EXISTS `Insert_Article`//
CREATE TRIGGER `Insert_Article` AFTER INSERT ON `Article`
 FOR EACH ROW insert into FullTextStore (`Table`, `PrimaryKey`, `ColumnName`, `Data`, `Created`)
select 'Article', NEW.ArticleID, 'Description', NEW.Description, UNIX_TIMESTAMP()
union
select 'Article', NEW.ArticleID, 'Keywords', NEW.Keywords, UNIX_TIMESTAMP()
//

但是……一定有更简单的方法吗?当我尝试使用时;要终止每个语句,它会以

But... There must be an easier way? When I try using ; to terminate each statement, it fails with

1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL version for the right syntax to use near 'select 'Article', NEW.ArticleID, 'Keywords', 'NEW.Keywords, UNIX_TIMESTAMP())' at line 1

我什至无法让多个更新语句起作用.

I can't even get multiple update statements to work.

如果有人能指出我做错了什么,那将是一个很大的帮助?

It'd be a great help if anyone could point out what i'm doing wrong?

干杯

加文

推荐答案

来自文档:创建触发器语法

trigger_stmt 是对当触发器激活时执行.如果你想执行多个语句,使用 BEGIN ... END复合语句结构.这还使您能够使用相同的内允许的陈述存储例程

trigger_stmt is the statement to execute when the trigger activates. If you want to execute multiple statements, use the BEGIN ... END compound statement construct. This also enables you to use the same statements that are allowable within stored routines

CREATE TRIGGER testref BEFORE INSERT ON test1
  FOR EACH ROW BEGIN
    INSERT INTO test2 SET a2 = NEW.a1;
    DELETE FROM test3 WHERE a3 = NEW.a1;
    UPDATE test4 SET b4 = b4 + 1 WHERE a4 = NEW.a1;
  END;

这篇关于触发器内有多个插入/更新语句?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Hibernate reactive No Vert.x context active in aws rds(AWS RDS中的休眠反应性非Vert.x上下文处于活动状态)
Bulk insert with mysql2 and NodeJs throws 500(使用mysql2和NodeJS的大容量插入抛出500)
Flask + PyMySQL giving error no attribute #39;settimeout#39;(FlASK+PyMySQL给出错误,没有属性#39;setTimeout#39;)
auto_increment column for a group of rows?(一组行的AUTO_INCREMENT列?)
Sort by ID DESC(按ID代码排序)
SQL/MySQL: split a quantity value into multiple rows by date(SQL/MySQL:按日期将数量值拆分为多行)