2014-04-09 55 views
0

我有這個:SQL觸發器不執行...做錯了什麼?

這是在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部分......這打破了觸發器。我試圖找出一個被認可的人是否已經擁有一個角色,如果他們這麼做了,就向該角色添加一個變量,說這不是他們的第一個角色。它說錯誤在IF EXISTS行,但我看不出錯誤...請幫忙嗎?

+0

語法對於觸發器和過程往往是特定於實現的。請標記您正在使用的SQL的方言。 – Turophile

+0

@Turophile SQL server 2012,MySQL,並在Navicat中使用 –

回答

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'