以下的文章主要向大家描述的是MySQL 分頁存儲過程的實際操作流程,我們是以介紹其實際應用代碼的方式來引出MySQL 分頁存儲過程的實際操作步驟,下面就是文章的主要內容的講述,望你會有所收獲。
DROP PROCEDURE IF EXISTS pr_pager;
- CREATE PROCEDURE pr_pager(
- IN p_table_name VARCHAR(1024),
- IN p_fields VARCHAR(1024),
- IN p_page_size INT,
- IN p_page_now INT,
- IN p_order_string VARCHAR(128),
- IN p_where_string VARCHAR(1024),
- OUT p_out_rows INT
- )
- NOT DETERMINISTIC
- SQL SECURITY DEFINER
COMMENT '分頁存儲過程'
BEGIN
定義變量
- DECLARE m_begin_row INT DEFAULT 0;
- DECLARE m_limit_string CHAR(64);
MySQL 分頁存儲過程中的構造語句
- SET m_begin_row = (p_page_now - 1) * p_page_size;
- SET m_limit_string = CONCAT(' LIMIT ', m_begin_row, ', ', p_page_size);
- SET @COUNT_STRING = CONCAT('SELECT COUNT(*) INTO @ROWS_TOTAL FROM ', p_table_name, ' ', p_where_string);
- SET @MAIN_STRING = CONCAT('SELECT ', p_fields, ' FROM ', p_table_name, ' ', p_where_string, ' ', p_order_string, m_limit_string);
預處理
- PREPARE count_stmt FROM @COUNT_STRING;
- EXECUTE count_stmt;
- DEALLOCATE PREPARE count_stmt;
- SET p_out_rows = @ROWS_TOTAL;
- PREPARE main_stmt FROM @MAIN_STRING;
- EXECUTE main_stmt;
- DEALLOCATE PREPARE main_stmt;
- END
以上的相關內容就是對MySQL 分頁存儲過程的介紹,望你能有所收獲。