手机
当前位置:查字典教程网 >编程开发 >mssql数据库 >分页查询 效率最高
分页查询 效率最高
摘要:复制代码代码如下:/*日期:2009-03-19功能:根据各种条件获取游戏国家任务列表数据*/Createprocedure[dbo].[P...

复制代码 代码如下:

/*

日期:2009-03-19

功能:根据各种条件获取 游戏国家任务 列表数据

*/

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

【分页查询 效率最高】相关文章:

sql 多表连接查询

SQL Server储过程加密和解密原理深入分析

分页 SQLServer存储过程

SqlServer 分页存储过程

分页存储过程代码

sql条件查询+排序

SQL Server 2000中的触发器使用

查询数据排名情况SQL

SQLserver 实现分组统计查询(按月、小时分组)

SQL连接查询介绍

精品推荐
分类导航