Home > Database > Mysql Tutorial > How to Parameterize the LIMIT Clause in MySQL Stored Procedures?

How to Parameterize the LIMIT Clause in MySQL Stored Procedures?

DDD
Release: 2024-11-03 05:54:03
Original
1073 people have browsed it

How to Parameterize the LIMIT Clause in MySQL Stored Procedures?

Parameterizing LIMIT Clause in MySQL Stored Procedures

Question:

In creating a paging class, you aim to pass two parameters to a MySQL stored procedure's LIMIT clause. However, attempting to use INT parameters and a query structure like the following yields an error:

<code class="sql">SELECT *
FROM
`MyTable`
LIMIT
MyFirstParamInt, MySecondParamInt</code>
Copy after login

Is there an alternative approach to achieve this parameterization, or is dynamic query construction and execution the only solution?

Answer:

Prior to MySQL 5.5.6:

Prior to version 5.5.6, parameterization of the LIMIT clause was not supported in MySQL stored procedures. As such, building the query dynamically and executing it was the only feasible method.

MySQL 5.5.6 and Above:

Starting from MySQL 5.5.6, it became possible to parameterize the LIMIT and OFFSET clauses using INTEGER parameters. To do this, simply pass the stored procedure parameters directly to LIMIT and OFFSET, as shown below:

<code class="sql">SELECT *
FROM
`MyTable`
LIMIT ? OFFSET ?</code>
Copy after login

In this example, ? represents the placeholders for the two INTEGER parameters that you intend to pass.

The above is the detailed content of How to Parameterize the LIMIT Clause in MySQL Stored Procedures?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template