views:

99

answers:

1

Here's a sample of what I'd like to do. The stored procedure fails to create while the LIMIT is present on the query. If I take the LIMIT off the procedure creates.

CREATE PROCEDURE LimitTest(IN in_start INTEGER UNSIGNED, IN in_limit INTEGER UNSIGNED)
BEGIN
   DECLARE no_more_results INTEGER DEFAULT 0;
   DECLARE var_result INTEGER UNSIGNED;
   DECLARE row CURSOR FOR SELECT id from test LIMIT in_start,in_limit;
   DECLARE CONTINUE HANDLER FOR NOT FOUND 
    SET no_more_results = 1;
   OPEN  row;
      FETCH  row INTO var_result;
      REPEAT 
         INSERT INTO results VALUES(var_result);
         FETCH  row INTO var_result;
      UNTIL no_more_results = 1
      END REPEAT;
   CLOSE row;
END
+3  A: 

The issue is that the number behind limit must be a constant and can't be a parameter which according to this bug report remains unfixed in later versions of MySQL. You could try the (ugly as hell) workaround outlined here:-

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;
Gavin Gilmour
I looked into that, but I couldn't figure out how to get my results into a cursor so that I could use them in a later insert.
ScArcher2