开发者

how to use limit with an input from a procedure

I want to use pollNo in limit, but it get an error:

error :Script line: 4 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 'pollNo,1 )as A join poll_data on A.id=poll_data.poll_id;

END' at line 6

DELIMITER $$

DROP PROCEDURE IF EXISTS `prj`.`ret_poll` $$
CREATE PROCEDURE `prj`.`ret_poll` (IN pollNo int)
BEGIN
selec开发者_JAVA百科t * from
(select * from poll limit pollNo,1 )as A
JOIN poll_data
on A.id=poll_data.poll_id;

END $$

DELIMITER ;


What version of MySQL are you using? I think this is fixed in 5.5 and forward, but prior versions need a workaround, since they want integer constants or prepared statements.

So, prior to 5.5 you should be able to do something like the following;

DELIMITER $$

DROP PROCEDURE IF EXISTS `prj`.`ret_poll` $$
CREATE PROCEDURE `prj`.`ret_poll` (IN pollNo int)
BEGIN
PREPARE STMT FROM "select * from (select * from poll limit ?,1 ) as A join poll_data on A.id=poll_data.poll_id";
SET @start = pollNo;

EXECUTE STMT USING @start;

END $$

DELIMITER ;

You can read more about it here: http://bugs.mysql.com/bug.php?id=11918


LIMIT requires an integer as input, not a column name. To do what you're talking about requires dynamic queries.

Here's a forum post that talks about executing dynamic SQL in MySQL.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜