sql分页操作

看到了网上关于分页的讲解  对最快的分页语句做了测试 还别说速度真快 总共6w条数据 速度确实so 快

前提是id是主键 或者是索引

declare @page int;--页数
declare @PageSize int;--单页显示数

select @page=1;
select @pageSize=1000;

select top (@pageSize) * from rdrecord rd
left join Vendor Ven on rd.cVenCode =Ven.cVenCode
where 
(id > 
	(
		select isnull(max(id),0) from 
			( select top ((@page-1)*@pageSize) id from rdrecord order by id ) RDSID
	)
)
order by id 

存储存过程
--sql分页存储过程
--@sqlstr  查询语句
--@currentpage 当前页码
--@pagesize每页信息数
--返回值
---1、记录数
---2、符合条件的记录集
CREATE procedure [dbo].[PagingQuery]
@sqlstr nvarchar(4000), --查询字符串
@currentpage int, --第N页
@pagesize int, --每页行数
@allrecords int OUTPUT  --返回的总记录数
as
set nocount on
declare @P1 int, --P1是游标的id
@rowcount int
exec sp_cursoropen @P1 output,@sqlstr,@scrollopt=1,@ccopt=1,@rowcount=@rowcount output
set @allrecords=@rowcount
set @currentpage=(@currentpage-1)*@pagesize+1
exec sp_cursorfetch @P1,16,@currentpage,@pagesize
exec sp_cursorclose @P1
set nocount off

GO

 

  

posted @ 2016-05-04 16:21  伪装大牛  阅读(359)  评论(0编辑  收藏  举报