MySQL Forums
Forum List  »  Newbie

Using a variable with LIMIT in SELECT statement
Posted by: CMS
Date: October 10, 2005 03:35PM

Is it possible, either directly or with a decent workaround to perform a LIMIT on a SELECT statement using a variable?

For example, I want the top N results, where N might be passed in to a stored procedure, or even issued from the command line...

CREATE PROCEDURE `xx`(IN N INT)
SELECT * FROM `Candidate` LIMIT N;

This fails at 'LIMIT N' because, apparently, LIMIT has to be followed by a literal constant.

Is there a way to do this, or do I need to construct the SQL in my client and give up on using a stored procedure whenever I need a variable number of results?

Thanks!
-Cj

Options: ReplyQuote


Subject
Written By
Posted
Using a variable with LIMIT in SELECT statement
CMS
October 10, 2005 03:35PM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.