MySql语法错误

时间:2013-01-06 06:13:39

标签: mysql sql stored-procedures

我在以下存储过程中出现错误语法错误:

DROP PROCEDURE IF EXISTS `SP_IncDecReferenceCount`;
CREATE DEFINER = `root`@`localhost` PROCEDURE `SP_IncDecReferenceCount`(pReferenceID int)
BEGIN
IF EXISTS(SELECT * from filesrefrences where filesrefrences.ReferenceID= pReferenceID and filesrefrences.RefrenceCount>1) then 
update filesrefrences
set 
    filesrefrences.RefrenceCount= filesrefrences.RefrenceCount-1
where 
        filesrefrences.ReferenceID= pReferenceID;
SELECT pReferenceID;
ELSE IF EXISTS(SELECT * from filesrefrences where filesrefrences.ReferenceID= pReferenceID) then 
DELETE from filesrefrences WHERE filesrefrences.ReferenceID = pReferenceID;
SELECT -77;
ELSE
SELECT -99;
end if ;
END;

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

1 个答案:

答案 0 :(得分:0)

您需要更改DELIMITER。例如,

DROP PROCEDURE IF EXISTS `SP_IncDecReferenceCount`;

DELIMITER &&

CREATE PROCEDURE `SP_IncDecReferenceCount`(pReferenceID int)
BEGIN
    IF EXISTS(SELECT * from filesrefrences where filesrefrences.ReferenceID= pReferenceID and filesrefrences.RefrenceCount>1) then 
        update filesrefrences
        set 
            filesrefrences.RefrenceCount= filesrefrences.RefrenceCount-1
        where 
                filesrefrences.ReferenceID= pReferenceID;
        SELECT pReferenceID;
    ELSE IF EXISTS(SELECT * from filesrefrences where filesrefrences.ReferenceID= pReferenceID) then 
        DELETE from filesrefrences WHERE filesrefrences.ReferenceID = pReferenceID;
        SELECT -77;
    ELSE
        SELECT -99;
    end if ;
END &&

DELIMITER ;