MySQL Paging stored procedure

Source: Internet
Author: User

CREATE PROCEDURE Commonpageproc (

In P_tablename VARCHAR (1024),
In P_fields VARCHAR (1024),
In P_pagesize INT,
In P_pageindex INT,
In P_orderfield VARCHAR (128),
In P_sqlwhere VARCHAR (1024),
Out P_totalrecord INT

)
Not deterministic
SQL SECURITY Definer
COMMENT ' Paging stored procedure '

BEGIN

/* Define variable */
DECLARE M_begin_row INT DEFAULT 0;
DECLARE m_limit_string CHAR (64);

/* Construct statement */
SET M_begin_row = (p_pageindex-1) * p_pagesize;
SET m_limit_string = CONCAT (' Limit ', M_begin_row, ', ', p_pagesize);
if (p_sqlwhere<> ") Then
Set P_sqlwhere=concat (' where ', p_sqlwhere);
End If;
If P_orderfield<> ' Then
Set P_orderfield=concat (' ORDER by ', P_orderfield);
End If;

SET @COUNT_STRING = CONCAT (' SELECT COUNT (*) into @ROWS_TOTAL from ', P_tablename, ', p_sqlwhere);
SET @MAIN_STRING = CONCAT (' SELECT ', P_fields, ' from ', P_tablename, ' ', P_sqlwhere, ', P_orderfield, m_limit_string);

/* Pre-treatment */
PREPARE count_stmt from @COUNT_STRING;
EXECUTE count_stmt;
Deallocate PREPARE count_stmt;
SET P_totalrecord = @ROWS_TOTAL;

PREPARE main_stmt from @MAIN_STRING;
EXECUTE main_stmt;
Deallocate PREPARE main_stmt;

END

MySQL Paging stored procedure

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.