递归更新触发器无法正常工作

时间:2014-03-05 06:20:49

标签: mysql sql triggers

我创建了2个表abcxyz。两个表都具有如下所示的相同数据。

abc

+------+-------+--------+
| roll | name  | status |
+------+-------+--------+
| 1    | john  | I      |
| 1    | ken   | I      |
| 1    | abel  | I      |
| 2    | aston | I      |
| 2    | ron   | I      |
+------+-------+--------+

xyz

+-------+-------+---------+
| roll1 | name1 | status1 |
+-------+-------+---------+
| 1     | john  | I       |
| 1     | ken   | I       |
| 1     | abel  | I       |
| 2     | aston | I       |
| 2     | ron   | I       |
+-------+-------+---------+

两个表格中的状态均可更新为'D''E'。假设在表abc中,如果John的状态变为'D',那么我需要John's滚动(即1)并更新所有状态xyz'D' roll1 = 1

同样,假设ron表中'E'的状态更改为xyz,则astonron状态应为'E'在表abc中。

这需要使用更新触发器来实现。我为这两个表创建了2个更新触发器。使用的触发器是

DELIMITER $$

DROP TRIGGER /*!50032 IF EXISTS */ `test2`.`abc_trigg`$$

create trigger `test2`.`abc_trigg` BEFORE UPDATE on `test2`.`abc`   
for each row BEGIN  
  IF @__disable_trigger is null THEN  
    SET @__disable_trigger = 1;  
    if new.status = 'D' then  
      update xyz set status1='D' where roll1 = new.roll;      
    elseif new.status = 'E' then   
      update xyz set status1='E' where roll1 = new.roll;  
    end if;  
  end if;  
end;  
$$  

DELIMITER ;

DELIMITER $$ 

DROP TRIGGER /*!50032 IF EXISTS */ `test2`.`xyz_trigg`$$  
create trigger `test2`.`xyz_trigg` BEFORE UPDATE on `test2`.`xyz`   
for each row BEGIN  
  IF @__disable_trigger is null THEN  
    SET @__disable_trigger = 1;  
    if new.status1 = 'D' then  
      update abc set status='D' where roll = new.roll1;  
    elseif new.status1 = 'E' then  
      update abc set status='E' where roll = new.roll1;  
    end if;  
  end if;  
end;  
$$  

DELIMITER ;  

但是这些触发器不正确,我没有得到预期的输出。请指导我解决问题并帮助我获得相关的输出?

0 个答案:

没有答案