msql 分页存储过程

mysql的分页存储过程,前台可以通过相应参数直接调用获取结果,也可以结合jqGrid的列表和分页器配置参数匹配使用

-- --------------------------------------------------------------------------------

-- Routine DDL

-- Note: comments before and after the routine body will not be stored by the server

-- --------------------------------------------------------------------------------

DELIMITER $$



CREATE DEFINER=`root`@`%` PROCEDURE `fenye`(

    IN p_table_name    VARCHAR(100),--表名    

    IN p_fields        VARCHAR(500),--所选字段 

    IN p_where_string  VARCHAR(500),--带where的过滤条件 

    IN p_order_string  VARCHAR(500),--带order的排序方式

    IN p_page_now      INT,       --现在所在页数 

    IN p_page_size     INT,      --每页的数量

    OUT p_out_rows     INT        --输出参数,共计记录数量

)

BEGIN

     /*定义变量*/

    DECLARE m_begin_row INT DEFAULT 0;

    DECLARE m_limit_string VARCHAR(128);

    /*构造语句*/    

    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

 

你可能感兴趣的:(存储过程)