mysql - 无法更新存储函数中的表 'attendance_tbl'/触发器删除错误

标签 mysql triggers

触发器:

    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/

相关文章:

mysql - 具有自动增量的存储过程 - 列计数异常

MySQL查询订单三差分字段

用于运行.txt文件的Mysql命令

mysql - Rails 5 返回所有记录并包括必要的关联

sql - PLSQL 触发器错误。 "is mutating, trigger/function may not see it"ORA-04091

mysql - 选择两个表并求和列值

sql-server - MSSQL 触发器 - 在 INSERT 上更新新插入的记录

mysql - JDBC 插入语句不在 MySQL 上启动触发器和函数

jquery - 我想在 jquery 中绑定(bind)键

mysql - 在 SQL 中声明变量的正确方法是什么?