SQL触发器没有执行......做错了什么?

时间:2014-04-09 01:35:40

标签: mysql sql triggers navicat

我有这个:

这是在chars_insert之前。

BEGIN
    IF EXISTS (SELECT * FROM chars WHERE accid = NEW.accid)
    BEGIN
            INSERT INTO `char_vars` SET `charid = NEW.charid, `varname` = `NotFirstChar`, `value` = 1;
    END

    INSERT INTO `char_equip`     SET `charid` = NEW.charid;
    INSERT INTO `char_exp`       SET `charid` = NEW.charid;
    INSERT INTO `char_jobs`      SET `charid` = NEW.charid;
    INSERT INTO `char_pet`  SET `charid` = NEW.charid;
    INSERT INTO `char_points`    SET `charid` = NEW.charid;
    INSERT INTO `char_profile`   SET `charid` = NEW.charid;
    INSERT INTO `char_storage`   SET `charid` = NEW.charid;
    INSERT INTO `char_inventory` SET `charid` = NEW.charid;
END

我刚刚添加了IF EXISTS部分......这打破了触发器。我试图找出一个accountid是否已经有一个角色,如果他们这样做,向创建的角色添加一个变量,说这不是他们的第一个。它说错误发生在IF EXISTS线上,但我看到没有错误......请帮忙吗?

1 个答案:

答案 0 :(得分:1)

尝试以下列方式为MySQL更改代码:

BEGIN
    IF EXISTS (SELECT * FROM chars WHERE accid = NEW.accid) THEN
            INSERT INTO `char_vars` SET `charid` = NEW.charid, `varname` = `NotFirstChar`, `value` = 1;
    END IF;

    INSERT INTO `char_equip`     SET `charid` = NEW.charid;
    INSERT INTO `char_exp`       SET `charid` = NEW.charid;
    INSERT INTO `char_jobs`      SET `charid` = NEW.charid;
    INSERT INTO `char_pet`  SET `charid` = NEW.charid;
    INSERT INTO `char_points`    SET `charid` = NEW.charid;
    INSERT INTO `char_profile`   SET `charid` = NEW.charid;
    INSERT INTO `char_storage`   SET `charid` = NEW.charid;
    INSERT INTO `char_inventory` SET `charid` = NEW.charid;
END

此外,如果您的NotFirstChars意味着是一个常量字符串(而非字段名称),则应更改引号:'NotFirstChars'