我正在使用一个名为inbox
的表,该表存储来自skills_matrix
的更改skills_matrix
。area_id
的请求。 skills_matrix
上的触发器执行正常,但是在更新inbox
时,skills_matrix
上的触发器被中断,出现以下错误:
ERROR 1442 (HY000): Can't update table 'inbox' in stored function/trigger because it
is already used by statement which invoked this stored function/trigger
我尝试添加额外的列skills_matrix
。area_change_pending
以在skills_matrix
触发条件中使用以防止语句执行。
CREATE DEFINER=`root`@`localhost` TRIGGER `skills_matrix_BEFORE_UPDATE` BEFORE UPDATE ON `skills_matrix` FOR EACH ROW BEGIN
IF (OLD.area_change_pending = 0 AND OLD.area_id != NEW.area_id) THEN
INSERT INTO `inbox` (`id`, `prev_area`, `prop_area`, `status`) VALUES ("0",OLD.area_id,NEW.area_id,"0");
SET NEW.area_id = OLD.area_id;
SET NEW.area_change_pending = 1;
END IF;
END
也在网上搜索:
其余的SQL:
| skill_matrix |
CREATE TABLE `skills_matrix` (
`id` int(11) NOT NULL,
`area_id` int(11) NOT NULL,
`area_change_pending` tinyint(1) NOT NULL DEFAULT 0 COMMENT '0 = Not pending request, 1 = pending request to change area',
PRIMARY KEY (`id`),
KEY `fk_skills_matrix_area1_idx` (`area_id`)
)
ENGINE=InnoDB DEFAULT CHARSET=utf8 |
CREATE DEFINER=`root`@`localhost` TRIGGER `skills_matrix_BEFORE_UPDATE` BEFORE UPDATE ON `skills_matrix` FOR EACH ROW BEGIN
IF (OLD.area_change_pending = 0 AND OLD.area_id != NEW.area_id) THEN
INSERT INTO `inbox` (`id`, `prev_area`, `prop_area`, `status`) VALUES ("0",OLD.area_id,NEW.area_id,"0");
SET NEW.area_id = OLD.area_id;
SET NEW.area_change_pending = 1;
END IF;
END
|收件箱|
CREATE TABLE `inbox` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`prev_area` int(11) DEFAULT NULL,
`prop_area` int(11) DEFAULT NULL,
`status` tinyint(1) DEFAULT 0 COMMENT '0 = pending acceptance, 1 = accepted',
PRIMARY KEY (`id`,`clock_number`)
)
ENGINE=InnoDB AUTO_INCREMENT=20 DEFAULT CHARSET=utf8 |
CREATE DEFINER=`root`@`localhost` TRIGGER `inbox_BEFORE_UPDATE` BEFORE UPDATE ON `inbox` FOR EACH ROW BEGIN
IF (NEW.status = 1) THEN
UPDATE `skills_matrix` SET `skills_matrix`.`area_change_pending` = 0, `skills_matrix`.`area_id` = NEW.prop_area;
DELETE FROM `loadall_portal`.`skills_matrix` WHERE `loadall_portal`.`skills_matrix`.`status` = 1;
END IF;
END
可以预期的是,当inbox
。status
更改为1
时,它将用{{1}更新skills_matrix
。area_id
。 }。NEW
,然后从prop_area
删除行并设置inbox
。skills_matrix
area_change_pending
,因为现在已接受区域更改。
即使在= 0
条件下OLD
。area_change_pending
阻止skills_matrix_BEFORE_UPDATE
触发时执行任何语句,为什么仍显示此错误?有一个更好的方法吗?我使用触发器是否正确,还是可以使用更简单的方法来实现? (首次使用触发器-非常感谢您的帮助!)