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

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
H
Hacker News: Front Page
P
Palo Alto Networks Blog
T
ThreatConnect
Apple Machine Learning Research
Apple Machine Learning Research
博客园_首页
T
True Tiger Recordings
P
Privacy & Cybersecurity Law Blog
B
Blog
IT之家
IT之家
Last Week in AI
Last Week in AI
F
Full Disclosure
Hacker News: Ask HN
Hacker News: Ask HN
C
Comments on: Blog
Microsoft Azure Blog
Microsoft Azure Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Microsoft Security Blog
Microsoft Security Blog
博客园 - 【当耐特】
N
News and Events Feed by Topic
NISL@THU
NISL@THU
腾讯CDC
雷峰网
雷峰网
Security Latest
Security Latest
李成银的技术随笔
M
Microsoft Research Blog - Microsoft Research
L
LangChain Blog
L
Lohrmann on Cybersecurity
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Check Point Blog
Y
Y Combinator Blog
Recent Announcements
Recent Announcements
博客园 - Franky
N
News | PayPal Newsroom
V
V2EX
A
About on SuperTechFans
The Register - Security
The Register - Security
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google Online Security Blog
Google Online Security Blog
MyScale Blog
MyScale Blog
Cisco Talos Blog
Cisco Talos Blog
Vercel News
Vercel News
WordPress大学
WordPress大学
C
Cyber Attacks, Cyber Crime and Cyber Security
The Hacker News
The Hacker News
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
爱范儿
爱范儿
A
Arctic Wolf
L
LINUX DO - 最新话题
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More

博客园 - 刺猬博客

利用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