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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了获取分页数据的存储过程模版相关的知识,希望对你有一定的参考价值。

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);

 

以上是关于获取分页数据的存储过程模版的主要内容,如果未能解决你的问题,请参考以下文章

使用存储过程进行数据分页

存储过程实现真分页效果

C#基于数据库存储过程的AJAX分页实例

分页库仅在房间数据库中存储第一次获取的值

使用Visual Studio Code自定义代码模版

动软代码生成器分页存储过程