当前位置:数据库 > MySQL >>

mysql 存储过程高效分页方法

我们提供这种mysql查询分页是一款要以针对上亿条大容易的数据进行分页并且是速度很快的,下面我们就来看看mysql存储过程的详细写法吧。

Create procedure [dbo].[PrGs_Nation_Task_GetList]
@PageSize int = 100, -- 每页显示记录条数,默认为100
@PageIndex int = 1, -- 当前提取要显示的页码,默认为1,数据库根据PageSize,PageIndex 计算返回一页数据
@RetTotal int output, -- 记录总数
@RetCount int output, -- 返回记录数
@RetPageIndex int output, -- 输出当前页码
@ReturnDesc varchar(128) output -- 返回操作结果描述
as
begin
set nocount on
set xact_abort on
set @RetTotal = 0
set @RetCount = 0
set @RetPageIndex = @PageIndex


-- 多条件取值
declare @Err int -- 错误
declare @PageCount int -- 总页数
declare @BeginRID int -- 开始行 Rid
declare @MaxRow int -- 最后行
select @RetTotal = count(*)
from NationTask
select @Err = @@ERROR
if @Err <> 0
begin
set @ReturnDesc = '提取国家任务总数失败!'
return -1
end
-- 如果无数据, 则返回空结果集
if @RetTotal = 0
begin
set @ReturnDesc = '当前条件无国家任务记录!'
return 1
end
-- 计算总页数
set @PageCount = @RetTotal / @PageSize
if @RetTotal % @PageSize > 0
begin
set @PageCount = @PageCount + 1
end
-- 超过总页数,则返回空结果集
if @PageIndex > @PageCount
begin
set @ReturnDesc = '当前条件无国家任务记录!'
return 1
end
-- 获取 要返回页面的 第一行纪录的 Rid
set @MaxRow = @PageSize * (@PageIndex - 1) + 1
set rowcount @MaxRow
select @BeginRID = Rid
from NationTask
order by Rid desc

-- 返回数据列表
set rowcount @PageSize
select Rid
,TaskName
,TaskTitle
,ImageID
,EffectID
,StartTime
from NationTask
where Rid <= @BeginRID
order by Rid desc
set @RetCount = @@rowcount
-- 结束
set @ReturnDesc = '提取国家任务列表成功!'
return 1
end

补充:数据库,mysql教程 
Oracle
MySQL
Access
SQLServer
DB2
Excel
SQLite
SYBASE
Postgres
如果你遇到数据库难题:
请访问www.zzzyk.com 试试
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,