Paging in MySQL stored procedure

Source: Internet
Author: User

2014-11-20

MySQL database, custom stored procedure queries the data in the table, with paging functionality. The following code is the example:

1DROP PROCEDURE IF EXISTS' sampledb '. ' Proc_getpageddataset ';2 3 CREATEDefiner=' Root ' @ '%`PROCEDURE' Proc_getpageddataset ' (4     inchTableNameVARCHAR( -),/*Table name5   in pageIndex INT,/* Current page*/6     inchPageSizeINT,/*number of records per page*/7Out PageCountINT,/*Total records divided by pages*/8Out Totalrecordcountint /*Total Record Count*/)9 BEGINTen    /*get the number of records in a table*/ One    Set @recordCount=0; A    Set @sql="'; -    Set @sql=CONCAT ('Select COUNT (*) into @recordCount from', TableName);
- Preparestmt from @sql;/*preprocessing custom SQL strings*/ the Executestmt/*execute a custom SQL statement*/ - deallocate Preparestmt/*Releasing preprocessing resources*/ - - SetTotalrecordcount= @recordCount;/*Total Record Count*/ + - /*calculate how many pages are returned*/ + Set @tmp=1; A Set @tmp=@recordCountMoD pageSize;/*take the remainder*/ at if @tmp=0 Then - SetPageCount=@recordCountDiv pageSize; - Else - SetPageCount=@recordCountDiv pageSize+ 1; - End if; - in /*pagination display of data*/ - Set @sql=CONCAT ('SELECT * from', TableName,'Limit', (PageIndex-1)*PageSize,',', pageSize);
to Preparestmt from @sql;/*preprocessing custom SQL strings*/ + Executestmt/*execute a custom SQL statement*/ - deallocate Preparestmt/*Releasing preprocessing resources*/ the END

Paging in MySQL 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.