mssql sql高效分页方法代码

php中文网
发布: 2016-06-07 17:47:30
原创
1040人浏览过

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

 

LANUX蓝脑商务网站系统
LANUX蓝脑商务网站系统

LANUX V1.0 蓝脑商务网站系统 适用于网店、公司宣传自己的品牌和产品。 系统在代码、页面方面设计简约,浏览和后台管理操作效率高。 此版本带可见即可得的html编辑器, 方便直观添加和编辑要发布的内容。 安装: 1.解压后,更换logo、分类名称、幻灯片的图片及名称和链接、联系我们等等页面。 2.将dbconfig.php里面的数据库配置更改为你的mysql数据库配置 3.将整个文件夹上传至

LANUX蓝脑商务网站系统 0
查看详情 LANUX蓝脑商务网站系统

--------生成查询语句--------
--此处@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

处理
  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   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

处理
  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   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

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号