放分 (通用分页 SQLServer存储过程)为分页数据头疼的人不用再担心了

比尔咔咔 2003-12-02 10:37:46
/*--用存储过程实现的分页程序

显示指定表、视图、查询结果的第X页
对于表中主键或标识列的情况,直接从原表取数查询,其它情况使用临时表的方法
如果视图或查询结果中有主键,不推荐此方法

--邹建 2003.09--*/

/*--调用示例
exec p_show '地区资料'

exec p_show '地区资料',5,3,'地区编号,地区名称,助记码','地区编号'
--*/

/*
因为要顾及通用性,所以对带排序的查询语句有一定要求.如果先排序,再出结果.就是:

exec p_show 'select top 100 percent * from 地区资料 order by 地区名称',5,3,'地区编号,地区名称,助记码','地区名称'


--查询语句加上:top 100 percent //top时
*/




if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[p_show]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[p_show]
GO

CREATE Proc p_show
@QueryStr nvarchar(4000), --表名、视图名、查询语句
@PageSize int=10, --每页的大小(行数)
@PageCurrent int=1, --要显示的页
@FdShow nvarchar (4000)='', --要显示的字段列表,如果查询结果有标识字段,需要指定此值,且不包含标识字段
@FdOrder nvarchar (1000)='' --排序字段列表
as
declare @FdName nvarchar(250) --表中的主键或表、临时表中的标识列名
,@Id1 varchar(20),@Id2 varchar(20) --开始和结束的记录号
,@Obj_ID int --对象ID
--表中有复合主键的处理
declare @strfd nvarchar(2000) --复合主键列表
,@strjoin nvarchar(4000) --连接字段
,@strwhere nvarchar(2000) --查询条件


select @Obj_ID=object_id(@QueryStr)
,@FdShow=case isnull(@FdShow,'') when '' then ' *' else ' '+@FdShow end
,@FdOrder=case isnull(@FdOrder,'') when '' then '' else ' order by '+@FdOrder end
,@QueryStr=case when @Obj_ID is not null then ' '+@QueryStr else ' ('+@QueryStr+') a' end

--如果显示第一页,可以直接用top来完成
if @PageCurrent=1
begin
select @Id1=cast(@PageSize as varchar(20))
exec('select top '+@Id1+@FdShow+' from '+@QueryStr+@FdOrder)
return
end

--如果是表,则检查表中是否有标识更或主键
if @Obj_ID is not null and objectproperty(@Obj_ID,'IsTable')=1
begin
select @Id1=cast(@PageSize as varchar(20))
,@Id2=cast((@PageCurrent-1)*@PageSize as varchar(20))

select @FdName=name from syscolumns where id=@Obj_ID and status=0x80
if @@rowcount=0 --如果表中无标识列,则检查表中是否有主键
begin
if not exists(select 1 from sysobjects where parent_obj=@Obj_ID and xtype='PK')
goto lbusetemp --如果表中无主键,则用临时表处理

select @FdName=name from syscolumns where id=@Obj_ID and colid in(
select colid from sysindexkeys where @Obj_ID=id and indid in(
select indid from sysindexes where @Obj_ID=id and name in(
select name from sysobjects where xtype='PK' and parent_obj=@Obj_ID
)))
if @@rowcount>1 --检查表中的主键是否为复合主键
begin
select @strfd='',@strjoin='',@strwhere=''
select @strfd=@strfd+',['+name+']'
,@strjoin=@strjoin+' and a.['+name+']=b.['+name+']'
,@strwhere=@strwhere+' and b.['+name+'] is null'
from syscolumns where id=@Obj_ID and colid in(
select colid from sysindexkeys where @Obj_ID=id and indid in(
select indid from sysindexes where @Obj_ID=id and name in(
select name from sysobjects where xtype='PK' and parent_obj=@Obj_ID
)))
select @strfd=substring(@strfd,2,2000)
,@strjoin=substring(@strjoin,5,4000)
,@strwhere=substring(@strwhere,5,4000)
goto lbusepk
end
end
end
else
goto lbusetemp

/*--使用标识列或主键为单一字段的处理方法--*/
lbuseidentity:
exec('select top '+@Id1+@FdShow+' from '+@QueryStr
+' where '+@FdName+' not in(select top '
+@Id2+' '+@FdName+' from '+@QueryStr+@FdOrder
+')'+@FdOrder
)
return

/*--表中有复合主键的处理方法--*/
lbusepk:
exec('select '+@FdShow+' from(select top '+@Id1+' a.* from
(select top 100 percent * from '+@QueryStr+@FdOrder+') a
left join (select top '+@Id2+' '+@strfd+'
from '+@QueryStr+@FdOrder+') b on '+@strjoin+'
where '+@strwhere+') a'
)
return

/*--用临时表处理的方法--*/
lbusetemp:
select @FdName='[ID_'+cast(newid() as varchar(40))+']'
,@Id1=cast(@PageSize*(@PageCurrent-1) as varchar(20))
,@Id2=cast(@PageSize*@PageCurrent-1 as varchar(20))

exec('select '+@FdName+'=identity(int,0,1),'+@FdShow+'
into #tb from'+@QueryStr+@FdOrder+'
select '+@FdShow+' from #tb where '+@FdName+' between '
+@Id1+' and '+@Id2
)

GO
...全文
109 102 打赏 收藏 转发到动态 举报
写回复
用AI写文章
102 条回复
切换为时间正序
请发表友善的回复…
发表回复
ldz112 2003-12-05
  • 打赏
  • 举报
回复
再看看
比尔咔咔 2003-12-05
  • 打赏
  • 举报
回复
up
gshope 2003-12-05
  • 打赏
  • 举报
回复
存储过程的唯一优势在于每次传输的数据量比较小,因为DataSet可以缓存数据,可DataGrid却不能缓存。
gshope 2003-12-05
  • 打赏
  • 举报
回复
to zat1978(蓟)
你说的有道理,不过假设每页10个,那就是100w/10=10w页,用户点10w次翻页,你的存储过程就要运行10w次,能比一次select 100w在性能上差多少呢??:)
zat1978 2003-12-05
  • 打赏
  • 举报
回复
我不用datagrid了
随意用任何一个数据控件都可以啊
分页是我自己算出来的
我只是用数据控件来显示我的当前页的数据
每次从数据库返回的数据只有当前的10或者20条
要知道sql server 的运算比asp.net不慢
而且100wselect一下的概念是一个恶梦啊
更不用说下载到客户端了
我每次只传到客户端10或者20条数据
yshawxp 2003-12-05
  • 打赏
  • 举报
回复
谢谢
gshope 2003-12-05
  • 打赏
  • 举报
回复
to zat1978(蓟)
你是否把DataSet放在Cache里了?并且在DataGrid_PageIndexChange事件里调用的是Cache里的DataSet呢??

看一下清华大学翻译的,微软出版社的<构建web解决方案--应用asp.net和ado.net>的174页,它提了两个优化方案,一个就是楼主说的,一个就是Cache数据DataSet。
zhaozhongju 2003-12-05
  • 打赏
  • 举报
回复
UP一下
liuyd 2003-12-05
  • 打赏
  • 举报
回复
感谢 孟子E章 !
cqnetboy 2003-12-05
  • 打赏
  • 举报
回复
学习中。。。。
zat1978 2003-12-05
  • 打赏
  • 举报
回复
100W数据我测试过,我这里有一个1G多的库,感觉用存储过程快的没法说,而用select简直就跑不起来了
比尔咔咔 2003-12-05
  • 打赏
  • 举报
回复
up
比尔咔咔 2003-12-05
  • 打赏
  • 举报
回复
upup
comtry 2003-12-05
  • 打赏
  • 举报
回复
look
daqiangli 2003-12-04
  • 打赏
  • 举报
回复
不错哟,
收藏
DataService 2003-12-04
  • 打赏
  • 举报
回复
刚才看了一下游标的概念和用法,越看越糊涂,游标可以处理分页吗?
DataService 2003-12-04
  • 打赏
  • 举报
回复
我希望听听各位对存储过程使用游标自定义分页的看法,以及比较
谢谢
比尔咔咔 2003-12-04
  • 打赏
  • 举报
回复
傳播
LengyuYuan 2003-12-04
  • 打赏
  • 举报
回复
感謝ing...
學習ing...
追梦 2003-12-04
  • 打赏
  • 举报
回复
接分
學習
傳播
加载更多回复(82)

62,025

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧