MySql测试版本:5.0.41-community-nt
DROP PROCEDURE IF EXISTS `PtBase`.`prPager`;
CREATE PROCEDURE `PtBase`.`prPager`(
OUT oRowsTotal INT, --输出记录总数
IN iTableName VARCHAR(800), --表名
IN iFields VARCHAR(800), --查询字段
IN iPageSize INT, --每页记录数
IN iPageNow INT, --当前页
IN iOrderString VARCHAR(100), --排序条件
IN iWhereString VARCHAR(800) --WHERE条件
)
NOT DETERMINISTIC
SQL SECURITY DEFINER
COMMENT ''
BEGIN
DECLARE _beginRow INT DEFAULT 0;
DECLARE _mainString VARCHAR(8000);
DECLARE _limitString VARCHAR(100);
DECLARE _countString VARCHAR(8000);
SET _beginRow = (iPageNow - 1) * iPageSize;
SET _limitString = CONCAT(' LIMIT ', beginRow, ', ', iPageSize);
SET _countString = CONCAT('SET oRowsTotal = SELECT COUNT(*) FROM ', iTableName, ' WHERE ', iWhereString);
SET _mainString = CONCAT('SELECT ', iFields, ' FROM ', iTableName, ' WHERE ', iWhereString, iOrderString, _limitString);
SET @countStr = _countString;
PREPARE countStmt FROM @countStr;
EXECUTE countStmt;
DEALLOCATE PREPARE countStmt;
SET @mainStr = _mainString;
PREPARE mainStmt FROM @mainStr;
EXECUTE mainStmt;
DEALLOCATE PREPARE mainStmt;
END;
CREATE PROCEDURE `PtBase`.`prPager`(
OUT oRowsTotal INT, --输出记录总数
IN iTableName VARCHAR(800), --表名
IN iFields VARCHAR(800), --查询字段
IN iPageSize INT, --每页记录数
IN iPageNow INT, --当前页
IN iOrderString VARCHAR(100), --排序条件
IN iWhereString VARCHAR(800) --WHERE条件
)
NOT DETERMINISTIC
SQL SECURITY DEFINER
COMMENT ''
BEGIN
DECLARE _beginRow INT DEFAULT 0;
DECLARE _mainString VARCHAR(8000);
DECLARE _limitString VARCHAR(100);
DECLARE _countString VARCHAR(8000);
SET _beginRow = (iPageNow - 1) * iPageSize;
SET _limitString = CONCAT(' LIMIT ', beginRow, ', ', iPageSize);
SET _countString = CONCAT('SET oRowsTotal = SELECT COUNT(*) FROM ', iTableName, ' WHERE ', iWhereString);
SET _mainString = CONCAT('SELECT ', iFields, ' FROM ', iTableName, ' WHERE ', iWhereString, iOrderString, _limitString);
SET @countStr = _countString;
PREPARE countStmt FROM @countStr;
EXECUTE countStmt;
DEALLOCATE PREPARE countStmt;
SET @mainStr = _mainString;
PREPARE mainStmt FROM @mainStr;
EXECUTE mainStmt;
DEALLOCATE PREPARE mainStmt;
END;