将 LIMIT 作为参数传递给 MySQL sproc
我正在创建一个分页类,需要将两个参数传递给我的 MySQL 存储过程作为 LIMIT 子句。
我将它们作为 INT 传递并尝试类似的操作,
SELECT *
FROM
`MyTable`
LIMIT
MyFirstParamInt, MySecondParamInt
但当我尝试保存存储过程时,它会给我一个错误。有没有办法做到这一点,我只是想念?或者我是否必须评估整个查询并执行它?
I'm creating a paging class and need to pass in two parameters to my MySQL stored procedure for the LIMIT clause.
I'm passing them in as INTs and trying something like this
SELECT *
FROM
`MyTable`
LIMIT
MyFirstParamInt, MySecondParamInt
it gives me an error when I try and save the sproc though. Is there a way to do this that I'm just missing? Or am I going to have to EVAL the whole query and EXECUTE it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
在 5.5.6 之前,
LIMIT
无法在 MySQL 存储过程中进行参数化。您需要动态构建查询并执行它。在 5.5.6 及更高版本中,您可以将存储过程参数作为参数传递给
LIMIT
和OFFSET
,只要它们是INTEGER
即可。Prior to 5.5.6,
LIMIT
could not be parameterized in MySQL stored procedures. You'd need to build the query dynamically and execute it.In 5.5.6 and above, you can just pass the stored procs parameters as arguments to
LIMIT
andOFFSET
as long as they areINTEGER
.我刚刚找到了一个可能有帮助的解决方案。
在存储过程中使用声明的变量并将它们设置为您的参数,
例如。
I just found a solution which may be helpful.
Use declared variables in your stored procedure and set them to your parameters
eg.
来自 http://dev.mysql.com/doc/refman/5.1/ en/select.html:
以下是准备好的语句示例,可能会对您有所帮助:
From http://dev.mysql.com/doc/refman/5.1/en/select.html:
Here's prepared statement example which might help you:
以下内容在 MySQL 5.5.35 中运行良好。它也适用于另一个过程,其中在
DECLARE 中使用相同的
语句。SELECT
。 。 。 CURSORThe following worked just fine in MySQL 5.5.35. It also worked in another procedure where the same
SELECT
was used within aDECLARE . . . CURSOR
statement.不带语句的分页:
pagination without statements:
最好的分页示例可以帮助您
调用
user_list
(v_private_key,v_user_id,v_pageIndex,v_limit,v_image_path,@o_rec_count,
@o_错误代码,
@o_error_message)
计算页面索引 SP
CREATE PROCEDUREcalculate_paging_index
(in_count,in_limit,in_page,@out_start_limit)
在此处输入代码`Best pagination example may help you
call
user_list
(v_private_key,v_user_id,v_pageIndex,v_limit,v_image_path,@o_rec_count,
@o_error_code,
@o_error_message)
calculate page index SP
CREATE PROCEDURE calculate_paging_index
(in_count,in_limit,in_page,@out_start_limit)
enter code here`简单的解决方案
尝试在存储过程中准备语句。
Simple solution
Try prepare statement in stored procedure.