mysql stored procedure: using declared vars in a l

2019-02-16 01:08发布

I have the following code:

delimiter ;

DROP PROCEDURE IF EXISTS ufk_test;
delimiter //
CREATE PROCEDURE ufk_test(IN highscoreChallengeId INT UNSIGNED)
BEGIN
DECLARE vLoopOrder INT UNSIGNED DEFAULT 5;
DECLARE vLoopLimit INT UNSIGNED DEFAULT 10;
select * from fb_user LIMIT vLoopOrder,vLoopLimit;
END//

delimiter ;

Mysql returns 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 'vLoopOrder,vLoopLimit;
END' at line 11

it seems that I cannot use declared variables in a LIMIT statement. is there any other way to overcome this ?

of course this is a simple example, here i could just put static numbers but I need to know if it's possible in any way to use any kind of variables with LIMIT.

Thanks

1条回答
贼婆χ
2楼-- · 2019-02-16 01:45

i use something like:

SET @s = CONCAT('SELECT * FROM table limit  ', vLoopOrder ', ', vLoopLimit); 
PREPARE stmt1 FROM @s; 
EXECUTE stmt1; 
DEALLOCATE PREPARE stmt1;
查看更多
登录 后发表回答