0

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

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
4

1 回答 1

0

你可以做

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);
于 2013-01-03T09:21:33.030 回答