mysql分页存储过程
SQL #mysql #分页 #存储过程2012-06-17 00:12
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); /*构造语句*/ 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导出表结构及表数据(mysqldump用法) 2012/06/17
- Mysql之Explain详解 2012/06/16
- mysql数据库表太大导致查询慢优化的几种方法 2012/06/16
- sql添加数据后返回受影响行数据 2012/05/21
- SQL批量插入数据的存储过程 2012/05/11
- SQL统计每天的登录次数 2012/05/09
- MySQL各种字段类型取值范围 2012/05/09
- oracle 常用SQL语句之二 2012/05/05
- oracle 常用SQL语句 2012/05/05
- SQL Server 2005 大容量日志恢复 2012/05/05