MySQL Forums :: Stored Procedures :: Variable in LIMIT clause


Advanced Search

Re: Variable in LIMIT clause
Posted by: Matthew Grdinic ()
Date: April 11, 2007 03:56PM

Per: William Chiquito http://forums.mysql.com/read.php?98,144558,144558#msg-144558

Passing dynamic variables in a stored procedure works per his example. I tired it out myself and all is well.

SAMPLE CODE (last two variables are the ones of interest):
IN model_1 varchar(40), IN model_2 varchar(40), IN model_3 varchar(40), IN filter varchar(20), IN p_low int, IN p_high int
)
BEGIN

IF model_1 = '' AND model_2 = '' AND model_3 = '' AND make_1 = '' AND make_2 = '' AND make_3 = '' THEN
SET @a = CONCAT('SELECT * FROM vehicleinventory ORDER BY ', filter, ' DESC LIMIT ', p_low, ',', p_high);
PREPARE stmtl FROM @a;
EXECUTE stmtl;
DEALLOCATE PREPARE stmtl;

You can also set variables as in:
IN model_1 varchar(40), IN model_2 varchar(40), IN model_3 varchar(40), IN filter varchar(20), IN p_low int, IN p_high int
)
BEGIN
SET @h = 10;

IF model_1 = '' AND model_2 = '' AND model_3 = '' AND make_1 = '' AND make_2 = '' AND make_3 = '' THEN
SET @a = CONCAT('SELECT * FROM vehicleinventory ORDER BY ', filter, ' ASC LIMIT ', @h, ',', p_high);
PREPARE stmtl FROM @a;
EXECUTE stmtl;
DEALLOCATE PREPARE stmtl;

Aslightly different syntax from the the MySQL manual: http://dev.mysql.com/doc/refman/5.0/en/select.html
For prepared statements, you can use placeholders (supported as of MySQL version 5.0.7). The following statements will return one row from the tbl table:

SET @a=1;
PREPARE STMT FROM 'SELECT * FROM tbl LIMIT ?';
EXECUTE STMT USING @a;

The following statements will return the second to sixth row from the tbl table:

SET @skip=1; SET @numrows=5;
PREPARE STMT FROM 'SELECT * FROM tbl LIMIT ?, ?';
EXECUTE STMT USING @skip, @numrows;


I am happy...for now : )



Edited 2 time(s). Last edit at 04/11/2007 04:06PM by Matthew Grdinic.

Options: ReplyQuote


Subject Views Written By Posted
Variable in LIMIT clause 37656 Markus Popp 06/01/2005 07:13PM
Re: Variable in LIMIT clause 16975 Markus Popp 06/13/2005 06:34PM
Re: Variable in LIMIT clause 13717 Joss 07/12/2005 03:59PM
Re: Variable in LIMIT clause 11740 Sergei Krupnov 07/13/2005 05:57PM
Re: Variable in LIMIT clause 9921 Per-Erik Martin 07/13/2005 05:58AM
Re: Variable in LIMIT clause 9631 Markus Popp 07/12/2005 04:07PM
Re: Variable in LIMIT clause 11160 Joss 07/15/2005 05:37AM
Re: Variable in LIMIT clause 7888 Markus Popp 07/13/2005 10:09AM
Re: Variable in LIMIT clause 7174 Markus Popp 07/15/2005 05:45AM
Re: Variable in LIMIT clause 7718 Kian Ryan 03/24/2006 04:48PM
Re: Variable in LIMIT clause 5836 Billy Huang 06/15/2006 07:23PM
Re: Variable in LIMIT clause 5674 Nick S 04/04/2007 08:56PM
Re: Variable in LIMIT clause 6065 Matthew Grdinic 04/11/2007 01:07PM
Re: Variable in LIMIT clause 13696 Matthew Grdinic 04/11/2007 03:56PM
Re: Variable in LIMIT clause 6011 Karl Seguin 05/02/2007 11:28AM
Re: Variable in LIMIT clause 5071 Wei Chen 03/16/2009 05:58PM
Re: Variable in LIMIT clause 5722 Peter Brawley 03/16/2009 07:21PM
Re: Variable in LIMIT clause 6871 Stephan Otto 04/29/2009 05:31PM


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.