MySql - 删除时触发

时间:2013-02-01 05:34:35

标签: mysql stored-procedures triggers

我有一个MySQL触发器,它在行删除时调用一个过程,但是当在表上删除行时它不会更新表。

我还有一个类似的触发器,它调用行更新和插入的过程,两者都在工作 - 它只是AFTER DELETE触发器,不起作用。

这是触发器:

CREATE
DEFINER=`root`@`localhost`
TRIGGER `mydb`.`update_lt_tr_del`
AFTER DELETE ON `mydb`.`leave`
FOR EACH ROW
BEGIN
    call update_lt(old.staff_leave_application_staff_id_staff, old.leave_type_id_leave_type);   
END$$

这是程序:

CREATE Procedure update_lt(IN staff_id INT(10), IN leave_type int(3))
BEGIN
    set @lt:= (SELECT Count(*) FROM `leave` WHERE (`leave`.active = 1) AND (`leave`.leave_type_id_leave_type = leave_type and `leave`.staff_leave_application_staff_id_staff = staff_id) );

    INSERT INTO `leave_taken`(leave_type_id_leave_type, staff_id_staff, days, updated) 
    VALUES (leave_type, staff_id, IFNULL(@lt,0), CURDATE())
    ON DUPLICATE KEY UPDATE days=IFNULL(@lt,0), updated = CURDATE(); 
END$$

请注意当我在插入或更新表时使用触发器调用它时,此过程有效。

工作触发器:

DELIMITER $$

USE `mydb`$$

CREATE
DEFINER=`root`@`localhost`
TRIGGER `mydb`.`update_lt_tr_in`
AFTER INSERT ON `mydb`.`leave`
FOR EACH ROW
BEGIN
call update_lt(new.staff_leave_application_staff_id_staff, new.leave_type_id_leave_type);
call update_remain_proc(new.staff_leave_application_staff_id_staff, new.leave_type_id_leave_type);  

END$$

CREATE
DEFINER=`root`@`localhost`
TRIGGER `mydb`.`update_lt_tr_up`
AFTER UPDATE ON `mydb`.`leave`
FOR EACH ROW
BEGIN
call update_lt(new.staff_leave_application_staff_id_staff, new.leave_type_id_leave_type); 
call update_remain_proc(new.staff_leave_application_staff_id_staff, new.leave_type_id_leave_type); 

END$$

0 个答案:

没有答案