视频1 视频21 视频41 视频61 视频文章1 视频文章21 视频文章41 视频文章61 推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37 推荐39 推荐41 推荐43 推荐45 推荐47 推荐49 关键词1 关键词101 关键词201 关键词301 关键词401 关键词501 关键词601 关键词701 关键词801 关键词901 关键词1001 关键词1101 关键词1201 关键词1301 关键词1401 关键词1501 关键词1601 关键词1701 关键词1801 关键词1901 视频扩展1 视频扩展6 视频扩展11 视频扩展16 文章1 文章201 文章401 文章601 文章801 文章1001 资讯1 资讯501 资讯1001 资讯1501 标签1 标签501 标签1001 关键词1 关键词501 关键词1001 关键词1501 专题2001 知道1 知道21 知道41 知道61 知道81 知道101 知道121 知道141 知道161 知道181 知道201 知道221 知道241 知道261 知道281
问答文章1 问答文章501 问答文章1001 问答文章1501 问答文章2001 问答文章2501 问答文章3001 问答文章3501 问答文章4001 问答文章4501 问答文章5001 问答文章5501 问答文章6001 问答文章6501 问答文章7001 问答文章7501 问答文章8001 问答文章8501 问答文章9001 问答文章9501
mssqlsql高效分页方法代码
2020-11-09 10:02:34 责编:小采
文档

mssql sql高效分页方法代码

alter procedure dbo.proc_listpage
(
@tblname nvarchar(200), ----要显示的表或多个表的连接
@fldname nvarchar(500) = '*', ----要显示的字段列表
@pagesize int = 1, ----每页显示的记录个数
@page int = 10, ----要显示那一页的记录
@pagecount int = 1 output, ----查询结果分页后的总页数
@counts int = 1 output, ----查询到的记录数
@fldsort nvarchar(200) = null, ----排序字段列表或条件
@sort bit = 0, ----排序方法,0为升序,1为降序(如果是多字段排列sort指代最后

一个排序字段的排列顺序(最后一个排序字段不加排序标记)--程序传参如:' sorta

asc,sortb desc,sortc ')
@strcondition nvarchar(1000) = null, ----查询条件,不需where
@id nvarchar(150), ----主表的主键
@dist bit = 0 ----是否添加查询字段的 distinct 默认0不添加/1添加
)
as
set nocount on
declare @sqltmp nvarchar(1000) ----存放动态生成的sql语句
declare @strtmp nvarchar(1000) ----存放取得查询结果总数的查询语句
declare @strid nvarchar(1000) ----存放取得查询开头或结尾id的查询语句

declare @strsorttype nvarchar(10) ----数据排序规则a
declare @strfsorttype nvarchar(10) ----数据排序规则b

declare @sqlselect nvarchar(50) ----对含有distinct的查询进行sql构造
declare @sqlcounts nvarchar(50) ----对含有distinct的总数查询进行sql构造


if @dist = 0
begin
set @sqlselect = 'select '
set @sqlcounts = 'count(*)'
end
else
begin
set @sqlselect = 'select distinct '
set @sqlcounts = 'count(distinct '+@id+')'
end


if @sort=0
begin
set @strfsorttype=' asc '
set @strsorttype=' desc '
end
else
begin
set @strfsorttype=' desc '
set @strsorttype=' asc '
end

--------生成查询语句--------
--此处@strtmp为取得查询结果数量的语句
if @strcondition is null or @strcondition='' --没有设置显示条件
begin
set @sqltmp = @fldname + ' from ' + @tblname
set @strtmp = @sqlselect+' @counts='+@sqlcounts+' from '+@tblname
set @strid = ' from ' + @tblname
end
else
begin
set @sqltmp = + @fldname + 'from ' + @tblname + ' where (1>0) ' +

@strcondition
set @strtmp = @sqlselect+' @counts='+@sqlcounts+' from '+@tblname + '

where (1>0) ' + @strcondition
set @strid = ' from ' + @tblname + ' where (1>0) ' + @strcondition
end

----取得查询结果总数量-----
exec sp_executesql @strtmp,n'@counts int out ',@counts out
declare @tmpcounts int
if @counts = 0
set @tmpcounts = 1
else
set @tmpcounts = @counts

--取得分页总数
set @pagecount=(@tmpcounts+@pagesize-1)/@pagesize

/**//**当前页大于总页数 取最后一页**/
if @page>@pagecount
set @page=@pagecount

--/*-----数据分页2分处理-------*/
declare @pageindex int --总数/页大小
declare @lastcount int --总数%页大小

set @pageindex = @tmpcounts/@pagesize
set @lastcount = @tmpcounts%@pagesize
if @lastcount > 0
set @pageindex = @pageindex + 1
else
set @lastcount = @pagesize

--//***显示分页
if @strcondition is null or @strcondition='' --没有设置显示条件
begin
if @pageindex<2 or @page<=@pageindex / 2 + @pageindex % 2 --前半部分数据

处理
begin
set @strtmp=@sqlselect+' top '+ cast(@pagesize as varchar(4))+' '+

@fldname+' from '+@tblname
+' where '+@id+' not in('+ @sqlselect+' top '+ cast(@pagesize*(@page-1)

as varchar(20)) +' '+ @id +' from '+@tblname
+' order by '+ @fldsort +' '+ @strfsorttype+')'
+' order by '+ @fldsort +' '+ @strfsorttype
end
else
begin
set @page = @pageindex-@page+1 --后半部分数据处理
if @page <= 1 --最后一页数据显示
set @strtmp=@sqlselect+' * from ('+@sqlselect+' top '+ cast(@lastcount as

varchar(4))+' '+ @fldname+' from '+@tblname
+' order by '+ @fldsort +' '+ @strsorttype+') as temptb'+' order by '+

@fldsort +' '+ @strfsorttype
else
set @strtmp=@sqlselect+' * from ('+@sqlselect+' top '+ cast(@pagesize as

varchar(4))+' '+ @fldname+' from '+@tblname
+' where '+@id+' not in('+ @sqlselect+' top '+ cast(@pagesize*(@page-2)

+@lastcount as varchar(20)) +' '+ @id +' from '+@tblname
+' order by '+ @fldsort +' '+ @strsorttype+')'

+' order by '+ @fldsort +' '+ @strsorttype+') as temptb'+' order by '+

@fldsort +' '+ @strfsorttype
end
end

else --有查询条件
begin
if @pageindex<2 or @page<=@pageindex / 2 + @pageindex % 2 --前半部分数据

处理
begin
set @strtmp=@sqlselect+' top '+ cast(@pagesize as varchar(4))+' '+

@fldname +' from '+@tblname
+' where '+@id+' not in('+ @sqlselect+' top '+ cast(@pagesize*(@page-1)

as varchar(20)) +' '+ @id +' from '+@tblname
+' where (1>0) ' + @strcondition + ' order by '+ @fldsort +' '+

@strfsorttype+')'
+' ' + @strcondition + ' order by '+ @fldsort +' '+ @strfsorttype
end
else
begin
set @page = @pageindex-@page+1 --后半部分数据处理
if @page <= 1 --最后一页数据显示
set @strtmp=@sqlselect+' * from ('+@sqlselect+' top '+ cast(@lastcount as

varchar(4))+' '+ @fldname+' from '+@tblname
+' where (1>0) '+ @strcondition +' order by '+ @fldsort +' '+

@strsorttype+') as temptb'+' order by '+ @fldsort +' '+ @strfsorttype
else
set @strtmp=@sqlselect+' * from ('+@sqlselect+' top '+ cast(@pagesize as

varchar(4))+' '+ @fldname+' from '+@tblname
+' where '+@id+' not in('+ @sqlselect+' top '+ cast(@pagesize*(@page-2)

+@lastcount as varchar(20)) +' '+ @id +' from '+@tblname
+' where (1>0) '+ @strcondition +' order by '+ @fldsort +' '+

@strsorttype+')'
+ @strcondition +' order by '+ @fldsort +' '+ @strsorttype+') as

temptb'+' order by '+ @fldsort +' '+ @strfsorttype
end
end

------返回查询结果-----
exec sp_executesql @strtmp
--print @strtmp
set nocount off

下载本文
显示全文
专题