Bluehost.com Web Hosting $6.95

Limit gives error in a Stored routine

This is a discussion on Limit gives error in a Stored routine within the MySQL Database forums, part of the Database Forums category; Hi group, I have this following stored routine which is not compiling. CREATE procedure Test3(IN a INT(2), IN ...


Go Back   Usenet Forums > Database Forums > MySQL Database

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 03-05-2007
jackal_on_work@yahoo.com
 
Posts: n/a
Default Limit gives error in a Stored routine

Hi group,

I have this following stored routine which is not compiling.

CREATE procedure Test3(IN a INT(2), IN b INT(2))
BEGIN
SELECT * FROM USERS LIMIT a, b;
END

I get the following error:

ERROR 1064 (42000): 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 'a, b;

Reply With Quote
  #2 (permalink)  
Old 03-05-2007
Axel Schwenke
 
Posts: n/a
Default Re: Limit gives error in a Stored routine

jackal_on_work@yahoo.com wrote:

> I have this following stored routine which is not compiling.
>
> CREATE procedure Test3(IN a INT(2), IN b INT(2))
> BEGIN
> SELECT * FROM USERS LIMIT a, b;
> END


This doesn't work. The parameters to LIMIT must be literals, not
variables. However there is a trick to make it work - convert the
statement to a prepared statement:

mysql> CREATE PROCEDURE p1(IN a INT, IN b INT)
-> BEGIN
-> SET @stmt=CONCAT('SELECT * FROM t1 LIMIT ', a, ',', b);
-> PREPARE s1 FROM @stmt;
-> EXECUTE s1;
-> END

mysql> select * from t1;
+------+
| c1 |
+------+
| 69 |
| 90 |
| 41 |
| 36 |
+------+

mysql> call p1(0,3);
+------+
| c1 |
+------+
| 69 |
| 90 |
| 41 |
+------+

mysql> call p1(1,2);
+------+
| c1 |
+------+
| 90 |
| 41 |
+------+


XL
--
Axel Schwenke, Support Engineer, MySQL AB

Online User Manual: http://dev.mysql.com/doc/refman/5.0/en/
MySQL User Forums: http://forums.mysql.com/
Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 03:44 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0