2013-02-03 152 views
0

请谁能告诉我为什么这个stored procedure不工作,我做错了什么。它不断给语法错误。准备语句查询语法错误

puserid and plimit是过程中传递的参数。

DECLARE uid BIGINT; 
DECLARE rangee TINYINT; 
SET @uid = puserid; 
SET @rangee = plimit * 15; 


PREPARE STMT FROM 
'IF((SELECT count(type) from notificationrecievers nr where 
nr.status=0 and nr.recieverid=?) = 0) THEN 
select nr.type,n.senderid,n.postid,u.name,nr.status 
from 
user u,notifications n,notificationrecievers nr where 
nr.recieverid=? and u.userid=n.senderid and 
nr.notificationid=n.notid order by n.notid desc 
limit 15 OFFSET ?; 
ELSE 
select nr.type,n.senderid,n.postid,u.name,nr.status 
from 
user u,notifications n,notificationrecievers nr where 
nr.recieverid=? and u.userid=n.senderid and nr.status=0 and 
nr.notificationid=n.notid order by n.notid desc; 
END IF;'; 

EXECUTE STMT USING @uid,@uid,@rangee,@uid; 

这是我得到的错误。

Error Code: 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 'IF((SELECT count(type) from notificationrecievers nr where nr.status=0 and nr.r' at line 1 
+0

我编辑了这个问题。 – Mj1992

+0

你什么时候得到这个错误?同时创建PROCEDURE或执行它?我可以创建这个程序就好了。 – SparKot

+0

我在执行过程时出现此错误。 – Mj1992

回答

0

您只能在准备好的语句中使用DML。 你可以这样做

IF condition THEN 
    set @q = 'your query text' 
ELSE 
    set @q = 'another query' 
END IF; 

PREPARE stmt FROM @q 
EXECUTE stmt using ... 
DEALLOCATE PREPARE stmt 

东西,你可以执行一些查询之前评估您的条件。 希望它有帮助

+0

thnx很多的帮助。 – Mj1992