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
4

1 回答 1

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

您可以在评估您的条件之前执行一些查询。希望能帮助到你

于 2013-02-04T08:31:38.047 回答