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

推荐订阅源

阮一峰的网络日志
阮一峰的网络日志
GbyAI
GbyAI
WordPress大学
WordPress大学
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
小众软件
小众软件
博客园 - 叶小钗
有赞技术团队
有赞技术团队
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
量子位
S
SegmentFault 最新的问题
博客园 - 三生石上(FineUI控件)
J
Java Code Geeks
S
Security Affairs
Cisco Talos Blog
Cisco Talos Blog
Jina AI
Jina AI
L
LINUX DO - 热门话题
Microsoft Security Blog
Microsoft Security Blog
aimingoo的专栏
aimingoo的专栏
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
S
Secure Thoughts
Stack Overflow Blog
Stack Overflow Blog
罗磊的独立博客
H
Hacker News: Front Page
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
T
Tenable Blog
The Last Watchdog
The Last Watchdog
P
Proofpoint News Feed
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
IT之家
IT之家
A
About on SuperTechFans
AWS News Blog
AWS News Blog
MyScale Blog
MyScale Blog
The Register - Security
The Register - Security
T
Threatpost
Last Week in AI
Last Week in AI
The Hacker News
The Hacker News
V
Visual Studio Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
S
Security @ Cisco Blogs
Project Zero
Project Zero
N
News | PayPal Newsroom
MongoDB | Blog
MongoDB | Blog
爱范儿
爱范儿
K
Kaspersky official blog
雷峰网
雷峰网
W
WeLiveSecurity
博客园 - 聂微东
D
DataBreaches.Net
博客园 - 司徒正美
V2EX - 技术
V2EX - 技术

博客园 - 刺猬博客

利用javascript验证各种格式 Asp.Net中使用水晶报表 SmartPager: a Flickr-style pager control with go-to-page popup layer Linux/UNIX 命令行大全完整版 分页存储过程 不可小瞧的using关键字 常用JS加密编码算法 安全验证码,简单加减法运算,机器无法识别的验证码 让你哈哈一下 中文汉字生成拼音 添加了代码着色的最新fckEditor 2.5.1 职场有多少IT精英透支健康和生命? 中国38%的IT从业人员靠微软生态系统生存 两个分页存储过程 仿163邮箱的alert提示,beta1.1 使用WebDeployment Project改善VS2005发布网站问题 (一) 基础 国外公司的大气与腾讯的小气 仿163信箱的alert提示,带效果预览图 用JS去掉打印的页眉页脚
通用高效分页存储过程代码
刺猬博客 · 2008-03-01 · via 博客园 - 刺猬博客

-- 获取指定页的数据 
CREATE PROCEDURE 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