码迷,mamicode.com
首页 > 数据库 > 详细

mysql分页存储过程

时间:2014-12-28 14:09:17      阅读:198      评论:0      收藏:0      [点我收藏+]

标签:

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 ‘分页存储过程‘
   
BEGIN

    /*定义变量*/
    DECLARE m_begin_row INT DEFAULT 0;
    DECLARE m_limit_string CHAR(64);

    /*构造语句*/   
    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);

    /*预处理*/
    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分页存储过程

标签:

原文地址:http://www.cnblogs.com/happyday56/p/4189861.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!