« Mouse handling with DOS functions in C++ | Home | Oracle: Renaming system-generated NOT NULL constraint names to intelligible ones »

MySQL 5.x: passing limits as stored procedure / function paramters

MySQL 5.x (at least 5.0.15 and earlier versions) does not allow using variables or procedure formal parameters with LIMIT. Here is a workaround:

CREATE PROCEDURE sp (
  IN LimitStart_ INT,
  IN LimitCnt_ INT
)
BEGIN
  SET @lim = CONCAT(' LIMIT ', LimitStart_, ',', LimitCnt_);
  SET @q = "SELECT mycol FROM mytable";

  SET @q = CONCAT(@q, @lim);
  PREPARE st FROM @q;
  EXECUTE st;
  DEALLOCATE PREPARE st;
END;

Topics: MySQL | Submitter: iconsultant

Comments

You must be logged in to post a comment.

Keep on coding