惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园 - Franky
N
Netflix TechBlog - Medium
Google Online Security Blog
Google Online Security Blog
月光博客
月光博客
量子位
酷 壳 – CoolShell
酷 壳 – CoolShell
V
V2EX
腾讯CDC
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
博客园 - 聂微东
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
M
MIT News - Artificial intelligence
Vercel News
Vercel News
The GitHub Blog
The GitHub Blog
Hugging Face - Blog
Hugging Face - Blog
博客园 - 【当耐特】
Apple Machine Learning Research
Apple Machine Learning Research
aimingoo的专栏
aimingoo的专栏
博客园 - 三生石上(FineUI控件)
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
MongoDB | Blog
MongoDB | Blog
H
Help Net Security
The Cloudflare Blog
Blog — PlanetScale
Blog — PlanetScale
F
Full Disclosure
G
Google Developers Blog
罗磊的独立博客
Jina AI
Jina AI
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Y
Y Combinator Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
J
Java Code Geeks
A
About on SuperTechFans
IT之家
IT之家
大猫的无限游戏
大猫的无限游戏
S
SegmentFault 最新的问题
有赞技术团队
有赞技术团队
GbyAI
GbyAI
雷峰网
雷峰网
T
The Blog of Author Tim Ferriss
The Register - Security
The Register - Security
U
Unit 42
D
Docker
Martin Fowler
Martin Fowler
L
LINUX DO - 热门话题
NISL@THU
NISL@THU
阮一峰的网络日志
阮一峰的网络日志
C
Cybersecurity and Infrastructure Security Agency CISA
博客园_首页
Google DeepMind News
Google DeepMind News

博客园 - Mason

[JavaScript]几种常用的表单输入判断 - Mason - 博客园 胡子决定编程语言运势[zt] 恢复SQL Server系统数据库 SQL server服务器大内存配置 《第3次机器人大战α》民间汉化版曝光 ASP.NET程序中常用的三十三种代码 - Mason - 博客园 探讨如何在有着1000万条数据的MS SQL SERVER数据库中实现快速的数据提取和数据分页 详述SQL Server 补丁安装常见问题 SQL SERVER实用技巧 SQL Server SQL语句导入导出大全 SQL Server数据库安全规划全攻略 曹操用什么统一全国?[转摘163] 几种SqlServer数据库分页方式[转载] SSO是门户的基础 关于MSSQL占用过多内存的问题 利用Visual C#打造一个平滑的进度条 Visual C# 3.0 新特性概览(一) .Net平台下开发中文语音应用程序 用C#生成中文汉字验证码的基本原理
分页SQL Server存储过程
Mason · 2006-01-22 · via 博客园 - Mason


/*--用存储过程实现的分页程序

显示指定表、视图、查询结果的第X页

对于表中主键或标识列的情况,直接从原表取数查询,其它情况使用临时表的方法

如果视图或查询结果中有主键,不推荐此方法*/

/*--调用示例

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