触发不起作用:

时间:2013-05-24 13:31:16

标签: mysql triggers

我在表PENDING上创建了触发器。待定表有3列 - uniqueId,duration和maxDuration。我有另一个表COUNT,有2列 - req_id,total

这是我的触发器 -

CREATE TRIGGER plus3second BEFORE INSERT
ON PENDING
FOR EACH ROW
BEGIN
DECLARE req_id varchar(25);
DECLARE total int(11);
DECLARE duration int(2);

SET req_id = SUBSTR(new.uniqueId, 1, 14);

Select total into total from COUNT where req_id = 'req_id';

IF total > 100 THEN

 SET duration = new.duration + 3;

  IF duration < new.maxDuration Then
     SET new.duration = duration;
  END IF;


END IF;


END

触发器已成功创建。我在COUNT和PENDING上发布了这些查询 -

insert into COUNT values ('77711422099653',200);
insert into PENDING (uniqueId, duration, maxDuration) values ('77711422099653919893277163', 3, 20);

但触发不起作用......问题出在哪里?

1 个答案:

答案 0 :(得分:0)

检查此触发器定义(名称冲突较少):

CREATE TRIGGER plus3second
   BEFORE INSERT
   ON PENDING
   FOR EACH ROW
BEGIN
   DECLARE tReqID     varchar(25);
   DECLARE tTotal      int(11);
   DECLARE tDuration   int(2);

   SET tReqID = SUBSTR(new.uniqueId, 1, 14);

   SELECT total
     INTO tTotal
     FROM COUNT
    WHERE req_id = tReqID;

   IF tTotal > 100
   THEN
      SET tDuration = new.duration + 3;

      IF tDuration < new.maxDuration
      THEN
         SET new.duration = tDuration;
      END IF;
   END IF;
END