在MySQL中创建触发器时出错

时间:2013-10-01 17:04:19

标签: mysql triggers

我是我的SQL初学者,我收到了错误

  

#1064 - 您的SQL语法出错;检查与MySQL服务器版本对应的手册,以获得正确的语法   '@nissue INT DEFAULT 0附近;设置@nissue:=(从代码中设置issue_id   代码'在第5行

代码

DELIMITER $$
CREATE TRIGGER ai_SMS
    AFTER INSERT ON SMS
    FOR EACH ROW 
BEGIN
       DECLARE @nissue INT DEFAULT 0;
       set @nissue := (set issue_id from Codes where code=NEW.msgbody);
 IF ( EXISTS(SELECT * FROM SMS_fltr AS s INNER JOIN Persons AS  p ON s.sender=p.mobile_number WHERE  s.numissue=@nissue and s.sender=NEW.sender))
       THEN
              UPDATE SMS_fltr set time=NEW.time ,code=NEW.msgbody where  numissue=@nissue and sender=NEW.sender;

       ELSE

              INSERT into SMS_fltr (sender,time,code,numissue) VAUES (NEW.sender,NEW.time,NEW.code,nissue);
       END IF
END$$
DELIMITER ;

我正在尝试为表格创建一个触发器。

任何人都能说出我犯了什么错误,解决方案是什么?

1 个答案:

答案 0 :(得分:0)

建议#1:不要声明

DELIMITER $$
CREATE TRIGGER ai_SMS
    AFTER INSERT ON SMS
    FOR EACH ROW 
BEGIN
       set @nissue := (set issue_id from Codes where code=NEW.msgbody);
 IF ( EXISTS(SELECT * FROM SMS_fltr AS s INNER JOIN Persons AS  p ON s.sender=p.mobile_number WHERE  s.numissue=@nissue and s.sender=NEW.sender))
       THEN
              UPDATE SMS_fltr set time=NEW.time ,code=NEW.msgbody where  numissue=@nissue and sender=NEW.sender;

       ELSE

              INSERT into SMS_fltr (sender,time,code,numissue) VAUES (NEW.sender,NEW.time,NEW.code,nissue);
       END IF
END$$
DELIMITER ;

建议#2:将其声明为常规变量

DELIMITER $$
CREATE TRIGGER ai_SMS
    AFTER INSERT ON SMS
    FOR EACH ROW 
BEGIN
       DECLARE nissue INT DEFAULT 0;
       set nissue := (set issue_id from Codes where code=NEW.msgbody);
 IF ( EXISTS(SELECT * FROM SMS_fltr AS s INNER JOIN Persons AS  p ON s.sender=p.mobile_number WHERE  s.numissue=@nissue and s.sender=NEW.sender))
       THEN
              UPDATE SMS_fltr set time=NEW.time ,code=NEW.msgbody where  numissue=@nissue and sender=NEW.sender;

       ELSE

              INSERT into SMS_fltr (sender,time,code,numissue) VAUES (NEW.sender,NEW.time,NEW.code,nissue);
       END IF
END$$
DELIMITER ;

试一试!!!