我写了一个触发器,在对表a执行操作后向表b中插入一行。 出于某种原因,如果我在插入后添加此触发器然后插入一行,则无效。但是,如果我将触发器添加为“更新后”并更新行,则它确实有效。
这是触发器代码。当我用'AFTER INSERT'替换'AFTER UPDATE'并进行插入时,插入新行时没有任何反应。创建触发器时我没有错误,我也没有尝试更新触发器所在的同一个表。 任何帮助表示赞赏! 谢谢, 仁
drop trigger if exists insertUndecided;
DELIMITER //
CREATE TRIGGER insertUndecided
AFTER UPDATE ON jiraissue
FOR EACH ROW
BEGIN
insert into nodeassociation (SOURCE_NODE_ID, SOURCE_NODE_ENTITY, SINK_NODE_ID, SINK_NODE_ENTITY, ASSOCIATION_TYPE, SEQUENCE)
select
NEW.id as SOURCE_NODE_ID,
'Issue' as SOURCE_NODE_ENTITY,
(select pv.id from projectversion pv
where pv.vname='undecided'
and pv.project=NEW.project ) as SINK_NODE_ID,
'Version' as SINK_NODE_ENTITY,
'IssueFixVersion' as ASSOCIATION_TYPE,
NULL as SEQUENCE
from dual where exists
(select pkey from jiraissue
where id=NEW.id and id not in
(select distinct source_node_id from nodeassociation
where source_node_entity='Issue' and SINK_NODE_ENTITY='Version'
and ASSOCIATION_TYPE='IssueFixVersion') );
END;//
DELIMITER ;
答案 0 :(得分:1)
在带有InnoDB表的MySQL 5.5.20上,我在简化测试中重现了你的情况。 两者,使用触发表中的结果插入引用新行alreay并使用NEW值直接插入工作正常
CREATE TABLE test1(a1 INT NOT NULL auto_increment, b1 INT, PRIMARY KEY (a1) );
CREATE TABLE test2(a1 INT, b1 INT);
CREATE TABLE test3(a1 INT, b1 INT);
DELIMITER ;;
CREATE TRIGGER testAI AFTER INSERT ON test1
FOR EACH ROW BEGIN
INSERT INTO test2 SET a1 = NEW.a1, b1 = NEW.b1;
INSERT INTO test3 ( a1, b1 ) SELECT a1, b1 FROM test1 WHERE a1 = NEW.a1;
END;
;;
DELIMITER ;
触发触发器
INSERT INTO test1 (b1) VALUES (1),(2),(3);
直接从新值插入
mysql> SELECT * FROM test2;
+------+------+
| a1 | b1 |
+------+------+
| 1 | 1 |
| 2 | 2 |
| 3 | 3 |
+------+------+
3 rows in set (0.00 sec)
从SELECT结果中插入
mysql> SELECT * FROM test3;
+------+------+
| a1 | b1 |
+------+------+
| 1 | 1 |
| 2 | 2 |
| 3 | 3 |
+------+------+
3 rows in set (0.00 sec)
mysql> SELECT VERSION();
+-----------+
| VERSION() |
+-----------+
| 5.5.20 |
+-----------+
1 row in set (0.00 sec)
因此,这适用于auto_increment和显式插入的值。 你的问题必须在其他地方