mysql没有终止触发器

时间:2013-11-26 22:46:53

标签: mysql triggers

我在我的mysql数据库中添加了一个触发器,但是当它被使用时它不会终止。我做错了什么?
我的添加触发器的查询:

DROP TRIGGER IF EXISTS insertResVals;
DELIMITER $$
create trigger insertResVals
AFTER INSERT
ON lastAccess
        for each row begin
                DECLARE done INT;
                DECLARE res varchar(50);
                DECLARE val double;
                DECLARE sig VARCHAR(140);
                DECLARE cur1 CURSOR FOR SELECT resource, value FROM resourceDefault;
                DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET done = 1;
                OPEN cur1;
                SET done = 0;
                read_loop: LOOP
                        FETCH cur1 INTO res, val;
                        IF NOT done THEN
                                LEAVE read_loop;
                        END IF;
                        insert into log
                        select concat(NEW.playerID, ", ", res, ", ", val);

                        if res<>null and val<>null THEN
                                INSERT INTO resourceValue VALUES(NEW.playerID, res, val);
                        ELSE
                                set done = 1;
                        END IF;
                END LOOP;

                CLOSE cur1;
        END $$
DELIMITER ;

非常感谢你 我试过调试,但插入日志表不起作用(我猜因为事务永远不会完成)。

1 个答案:

答案 0 :(得分:0)

问题必须出在以下代码行中:

...
if res<>null and val<>null THEN
...

因为它总是NULL,并且计算结果为FALSE。

<>更改12.3.2. Comparison Functions and Operators IS NOT NULL

...
IF res IS NOT NULL AND val IS NOT NULL THEN
...

<强>更新

以下触发器没有问题:

/* CODE FOR DEMONSTRATION PURPOSES */

/* Trigger structure for table `lastaccess` */

DROP TRIGGER IF EXISTS `insertResVals`;

DELIMITER $$

CREATE TRIGGER `insertResVals` AFTER INSERT ON `lastaccess`
FOR EACH ROW
BEGIN
    DECLARE done INT default 0;
    DECLARE res varchar(50);
    DECLARE val double;
    DECLARE sig VARCHAR(140);
    DECLARE cur1 CURSOR FOR SELECT resource, value FROM resourcedefault;
    DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET done = 1;
    OPEN cur1;
    SET done = 0;
    read_loop: LOOP
        FETCH cur1 INTO res, val;
        IF done THEN
            LEAVE read_loop;
        END IF;
        insert into log
        select concat(NEW.playerid, ", ", res, ", ", val);
        if res IS NOT null and val IS NOT null THEN
            INSERT INTO resourcevalue VALUES(NEW.playerid, res, val);
        ELSE
            set done = 1;
        END IF;
    END LOOP;
    CLOSE cur1;
END$$

DELIMITER ;

SQL Fiddle demo