The actual operation process and code of the MySQL paging Stored Procedure

Source: Internet
Author: User

The following articles mainly describe the actual operation process of the MySQL paging stored procedure. We will introduce the actual application code to introduce the actual operation steps of the MySQL paging stored procedure, the following describes the main content of the article. I hope you will gain some benefits.

Drop procedure if exists pr_pager;

 
 
  1. CREATE PROCEDURE pr_pager(  
  2. IN p_table_name VARCHAR(1024),   
  3. IN p_fields VARCHAR(1024),   
  4. IN p_page_size INT,   
  5. IN p_page_now INT,   
  6. IN p_order_string VARCHAR(128),   
  7. IN p_where_string VARCHAR(1024),   
  8. OUT p_out_rows INT   
  9. )  
  10. NOT DETERMINISTIC  
  11. SQL SECURITY DEFINER  

COMMENT 'paging stored Process'

BEGIN

Define Variables

 
 
  1. DECLARE m_begin_row INT DEFAULT 0;  
  2. DECLARE m_limit_string CHAR(64); 

MySQL construction statement in paging storage process

 
 
  1. SET m_begin_row = (p_page_now - 1) * p_page_size;  
  2. SET m_limit_string = CONCAT(' LIMIT ', m_begin_row, ', ', p_page_size);  
  3. SET @COUNT_STRING = CONCAT('SELECT COUNT(*) INTO @ROWS_TOTAL FROM ', p_table_name, ' ', p_where_string);  
  4. SET @MAIN_STRING = CONCAT('SELECT ', p_fields, ' FROM ', p_table_name, ' ', p_where_string, ' ', p_order_string, m_limit_string); 

Preprocessing

 
 
  1. PREPARE count_stmt FROM @COUNT_STRING;  
  2. EXECUTE count_stmt;  
  3. DEALLOCATE PREPARE count_stmt;  
  4. SET p_out_rows = @ROWS_TOTAL;  
  5. PREPARE main_stmt FROM @MAIN_STRING;  
  6. EXECUTE main_stmt;  
  7. DEALLOCATE PREPARE main_stmt;  
  8. END  

The above content is an introduction to the MySQL paging storage process. I hope you will get some benefits.

Related Article

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.