首页 > 代码库 > 获取分页数据的存储过程模版

获取分页数据的存储过程模版

drop procedure if exists proc_getPageRecords;
create procedure proc_getPageRecords(
in tableName varchar(30),  -- like:Student
in selectPart varchar(200),  -- like:StuId,Name
in wherePart varchar(200),  -- like:StuId=4&&Name in("ff","bc","dd")
in sortClm varchar(30),  -- like CreateTime
in isAscSort bit,
in pageIndex int,  -- like:1  // 第二页,其实这里可以直接用startRow,pageSize换成rowCount
in pageSize int)  -- like:100   // 每页100条
BEGIN
set @startRow = pageIndex*pageSize;

if(isAscSort) THEN
set @sortPart = CONCAT("order by ",sortClm," asc");  -- order by CreateTime asc
ELSE
set @sortPart = CONCAT("order by ",sortClm," desc");
end if;
/* select StuId,Name from Student where StuId=4&&Name in("ff","bc","dd") order by CreateTime asc limit 100,100;*/
set @sqlString = CONCAT("select ",selectPart," from ",tableName," where ",wherePart," ",@sortPart," limit ",@startRow,",",pageSize,";");
prepare sqlstmt from @sqlString;
execute sqlstmt;
DEALLOCATE PREPARE sqlstmt;
END

call proc_getPageRecords("Student","*","Name=朱七","CreateTime",0,0,2);

 

获取分页数据的存储过程模版