MySQL:创建触发器时出现语法错误

时间:2015-07-08 11:52:51

标签: php mysql

为什么我的sql语法会抛出错误?

CREATE TRIGGER info
AFTER INSERT ON inbox
FOR EACH ROW BEGIN if SUBSTRING(new.TextDecoded,1,6)='telkom' then
INSERT INTO outbox ( DestinationNumber, Coding, TextDecoded, CreatorID )VALUES ( new.SenderNumber, 'Default_No_Compression', (SELECT nama from data_dosen WHERE kode = SUBSTRING(new.TextDecoded,8,10)), '1');

else
INSERT INTO outbox (DestinationNumber, Coding, TextDecoded, CreatorID) VALUES (new.SenderNumber, 'Default_No_Compression', 'Maaf format sms Anda salah. Ketik telkom<spasi>kode dosen', '1');

end if;

END$$
  

您的SQL语法有错误;检查与MySQL服务器版本对应的手册,以便在第4行的''附近使用正确的语法

1 个答案:

答案 0 :(得分:0)

您的触发器之前没有分隔符定义。分隔符通知MySQL哪些字符将结束您的触发器。如果没有它,MySQL会将分号视为默认分隔符,并在代码中的第一个分号时失败。看看这个:

DELIMITER $$
CREATE TRIGGER info
AFTER INSERT ON inbox
FOR EACH ROW BEGIN if SUBSTRING(new.TextDecoded,1,6)='telkom' then
    INSERT INTO outbox ( DestinationNumber, Coding, TextDecoded, CreatorID )VALUES ( new.SenderNumber, 'Default_No_Compression', (SELECT nama from data_dosen WHERE kode = SUBSTRING(new.TextDecoded,8,10)), '1');

else
    INSERT INTO outbox (DestinationNumber, Coding, TextDecoded, CreatorID) VALUES (new.SenderNumber, 'Default_No_Compression', 'Maaf format sms Anda salah. Ketik telkom<spasi>kode dosen', '1');

end if;

END$$
DELIMITER ;