SQL server分页的三种方法

一、Entity Framework的Linq语句的分页写法:

 var datacount = test.OrderBy(t => t.testID)
 .Skip(pageSize * (pageIndex - 1))
 .Take(pageSize).ToList();

二、SQL Server分页的SQL语句写法:

select
top (需要显示的条目数) *
from
DBTest
where TestID not in
(select top (需要剔除的条目数) TestID from DBTest)

三、SQL Server分页的存储过程写法:

第一种:

create proc proc_TestPage
@PageIndex int --第几页
@PageSize int --每页显示的条数
@pageCount int output	--总的页数,因为需要显示页数,因此是个输出参数
as
declare @datacount int	--总数据条数
select @datacount=count(*) from DBTest--获得总数据条数值并赋给参数
set @pageCount=ceiling(1.0*@datacount/@pageSize)	--获得总页数,并赋给参数
select top(@PageSize) * from DBTest where TestID not int (select top(@PageSize*(@PageIndex-1)) from DBTest)
select @PageCount=Count(*) from DBTest

第二种:

create proc P_Test	--创建存储过程P_Test
@pageSize int,	--每页数据条数
@pageIndex int,	--当前页数(页码)
@pageCount int output	--总的页数,因为需要显示页数,因此是个输出参数
as
declare @datacount int	--总数据条数
select @datacount=count(*) from DBTest --获得总数据条数值并赋给参数
set @pageCount=ceiling(1.0*@datacount/@pageSize)	--获得总页数,并赋给参数
--接下来是获得指定页数据
select * from
(select *,row_number() over(order by TestID ) as num from DBTest) as temp
where num between @pageSize*(@pageIndex-1)+1 and @pageSize*@pageIndex
作者:TomLucas原文地址:https://www.cnblogs.com/lucasDC/p/17233400.html

%s 个评论

要回复文章请先登录注册