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

推荐订阅源

Latest news
Latest news
T
Troy Hunt's Blog
V
Vulnerabilities – Threatpost
L
LINUX DO - 热门话题
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Simon Willison's Weblog
Simon Willison's Weblog
V
V2EX
博客园 - 司徒正美
B
Blog RSS Feed
AWS News Blog
AWS News Blog
MyScale Blog
MyScale Blog
Scott Helme
Scott Helme
Cisco Talos Blog
Cisco Talos Blog
Last Week in AI
Last Week in AI
NISL@THU
NISL@THU
博客园 - Franky
P
Proofpoint News Feed
博客园_首页
C
CERT Recently Published Vulnerability Notes
雷峰网
雷峰网
S
Schneier on Security
P
Proofpoint News Feed
Hugging Face - Blog
Hugging Face - Blog
G
GRAHAM CLULEY
博客园 - 三生石上(FineUI控件)
月光博客
月光博客
WordPress大学
WordPress大学
The Hacker News
The Hacker News
T
Threatpost
阮一峰的网络日志
阮一峰的网络日志
A
Arctic Wolf
Microsoft Azure Blog
Microsoft Azure Blog
T
The Exploit Database - CXSecurity.com
Engineering at Meta
Engineering at Meta
罗磊的独立博客
T
The Blog of Author Tim Ferriss
D
Darknet – Hacking Tools, Hacker News & Cyber Security
I
Intezer
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
K
Kaspersky official blog
SecWiki News
SecWiki News
云风的 BLOG
云风的 BLOG
美团技术团队
C
Cybersecurity and Infrastructure Security Agency CISA
博客园 - 【当耐特】
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Security Latest
Security Latest
C
Cyber Attacks, Cyber Crime and Cyber Security
B
Blog
S
Security Affairs

博客园 - yfcomeon

浅谈C#托管程序中的资源释放问题 (转载) 静态构造函数的执行时机 详解javascript类继承机制的原理 Attributes in C# 组装机 散列基本概念知识点 C#的6种常用集合类大比拼 C#中的集合类 C#中的集合类 C#里的委托和事件实现 C#方法修饰符 C#类和接口及值类型和引用类型的区别 vc调试器 变量的域 SQL数据库空间不足怎么办 分页 B树算法 转:sql server系统表详细说明 转:sql server系统表详细说明
通用高效分页存储过程
yfcomeon · 2007-11-08 · via 博客园 - yfcomeon

-- 获取指定页的数据 
CREATE PROCEDURE dbo.pagination

@tblName varchar(255), -- 表名 
@strGetFields varchar(1000= '*'-- 需要返回的列 
@fldName varchar(255)=''-- 排序的字段名 
@PageSize int = 10-- 页尺寸 
@PageIndex int = 1-- 页码 
@doCount bit = 0-- 返回记录总数, 非 0 值则返回 
@OrderType bit = 0-- 设置排序类型, 非 0 值则降序 
@strWhere varchar(1500= '' -- 查询条件 (注意: 不要加 where) 

AS 

declare @strSQL varchar(5000-- 主语句 
declare @strTmp varchar(110-- 临时变量 
declare @strOrder varchar(400-- 排序类型 
if @doCount != 0 
begin 
if @strWhere !='' 
set @strSQL = 'select count(*) as Total from [' + @tblName + '] where '+@strWhere 
else 
set @strSQL = 'select count(*) as Total from [' + @tblName + ']' 
end 
--以上代码的意思是如果@doCount传递过来的不是0,就执行总数统计。以下的所有代码都是@doCount为0的情况 

else 
begin 
if @OrderType != 0 
begin 
set @strTmp = '<(select min' 
set @strOrder = ' order by [' + @fldName +'] desc' 
--如果@OrderType不是0,就执行降序,这句很重要! 
end 

else 
begin 
set @strTmp = '>(select max' 
set @strOrder = ' order by [' + @fldName +'] asc' 
end 
if @PageIndex = 1 
begin 
if @strWhere != '' 
set @strSQL = 'select top ' + str(@PageSize+' '+@strGetFields+ ' from [' + @tblName + '] where ' + @strWhere + ' ' + @strOrder 
else 
set @strSQL = 'select top ' + str(@PageSize+' '+@strGetFields+ ' from ['+ @tblName + ''+ @strOrder 
--如果是第一页就执行以上代码,这样会加快执行速度 
end 

else 
begin 
--以下代码赋予了@strSQL以真正执行的SQL代码 
set @strSQL = 'select top ' + str(@PageSize+' '+@strGetFields+ ' from [' 
+ @tblName + '] where [' + @fldName + ']' + @strTmp + '(['+ @fldName + ']) from (select top ' + str((@PageIndex-1)*@PageSize+ ' ['+ @fldName + '] from [' + @tblName + ']' + @strOrder + ') as tblTmp)'+ @strOrder 
if @strWhere != '' 
set @strSQL = 'select top ' + str(@PageSize+' '+@strGetFields+ ' from [' 
+ @tblName + '] where [' + @fldName + ']' + @strTmp + '([' 
+ @fldName + ']) from (select top ' + str((@PageIndex-1)*@PageSize+ ' [' 
+ @fldName + '] from [' + @tblName + '] where ' + @strWhere + ' ' 
+ @strOrder + ') as tblTmp) and ' + @strWhere + ' ' + @strOrder 
end 
end 
exec (@strSQL)
GO