准备语句查询语法错误

时间:2013-02-03 21:38:16

标签: mysql

请有人告诉我为什么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

1 个答案:

答案 0 :(得分: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

您可以在评估条件之前执行一些查询。 希望它有所帮助