触发器:
DELIMITER $$
USE `smartclass_dbv2`$$
CREATE TRIGGER `delete_attendance_on_holiday`
AFTER INSERT ON `attendance_tbl`
FOR
EACH ROW
BEGIN
DELETE T1
FROM
`attendance_tbl` T1
LEFT JOIN
announcement_tbl T2
ON
old.T1.date = T2.announcement_date
WHERE
old.T1.date = T2.announcement_date
and
announcement_tbl.announcement_description = 'holiday';
END$$
the scenario is any attendance on the day that is holiday will be deleted. In the announcement_tbl, it contains announcement_date, announcement_description, announcement_date. if the date of the announcement is same with the date on the attendance table provided that the description is holiday, it will void/delete the attendance rows on the attendance table. HOWEVER, when im inserting data on the ATTENDANCE_tbl, error happens. Can u help me figure out the problem?
这是错误:
“无法更新存储函数/触发器中的表‘attendance_tbl’,因为它已被调用此存储函数/触发器的语句使用。””
最佳答案
您不需要删除所有 出席的行,只需删除正在插入的行。为此,您在插入时出错:
DELIMITER $$
USE `smartclass_dbv2`$$
CREATE TRIGGER `delete_attendance_on_holiday` BEFORE INSERT ON `attendance_tbl`
FOR EACH ROW
BEGIN
IF EXISTS (SELECT 1
FROM announcement_tbl a
WHERE a.announcement_description = 'holiday' AND
a.announcement_date = new.date
) THEN
SIGNAL SQLSTATE '45000', message_text = 'Insert failed; date on holiday';
END IF;
END$$
您可能需要一个类似的触发器用于更新
。
关于mysql - 无法更新存储函数中的表 'attendance_tbl'/触发器删除错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49708667/