MySQL does not support variables with LIMIT in stored procedures

May 17, 2007 – 7:30 pm

After banging my head against a wall for a while, I finally found this thread from June 2005 in the MySQL support forums regarding using variables in the LIMIT clause. They don’t support it yet. There’s no time table that indicates when it will be supported either. It’s very unfortunate, I was looking forward to benchmarking the difference between our ad hoc code and the stored proc version.

This code will not work:

SELECT field from table LIMIT limit_var

Very unfortunate.

These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Reddit
  1. 3 Responses to “MySQL does not support variables with LIMIT in stored procedures”

  2. you can prepare an statement and then execute it with a variable limit, e.g.,

    CREATE PROCEDURE `All`(in off int, in lmt int)
    BEGIN

    set @a = off;
    set @b = lmt;
    prepare stmt from “select * from table1 limit ?,?”;
    execute stmt using @a,@b;

    END $$

    By Federico on Jul 17, 2007

  3. Sorry but according the manual you cannot use prepare or execute instructions on a procedure or function

    By proteo2000 on Jul 17, 2007

  4. what can I say? give it a try, it is working for me on a MySQL 5.0.37 version server running on Windows

    By Federico on Jul 18, 2007

Post a Comment