存储过程::navicat



我在MySql(navicat)中有以下存储过程,但我需要测试RefrenceCount在更新操作之后,当我在更新语句之后添加if。语法错误发生

CREATE PROCEDURE `SP_IncDecReferenceCount`(pReferenceID int,pIncValue int)
BEGIN
update filesrefrences
set 
    filesrefrences.RefrenceCount= filesrefrences.RefrenceCount+pIncValue 
where 
        filesrefrences.ReferenceID= pReferenceID;
IF Not EXISTS(SELECT * from filesrefrences where filesrefrences.ReferenceID= pReferenceID and filesrefrences.RefrenceCount>0) then 
DELETE from filesrefrences WHERE filesrefrences.ReferenceID = pReferenceID;
end if 
END
[Err] 1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'END' at line 11

你可以做

update filesrefrences
set 
    filesrefrences.RefrenceCount= filesrefrences.RefrenceCount+IF(pIncValue > 0,pIncValue,1)
where 
        filesrefrences.ReferenceID= pReferenceID;

update filesrefrences
    set 
        filesrefrences.RefrenceCount= filesrefrences.RefrenceCount+pIncValue
    where 
            (filesrefrences.ReferenceID= pReferenceID) AND (pIncValue > 0);

相关内容

  • 没有找到相关文章

最新更新