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

推荐订阅源

酷 壳 – 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

博客园 - wucf2004

教你一招 - Misc类型插件的妙用(附带插件源码) 如何在nopcommerce3.3注册页面添加密码强度检查仪? nopcommerce之权限模块 nopcommerce3.3简洁版 nopcommerce里面的@Html.Widget("home_page_top") 是什么? 教你一招 - 如何给nopcommerce增加一个类似admin的area - wucf2004 教你一招 - 如何给nopcommerce做一套自己的主题 nopcommerce之移动版简介 - wucf2004 网银在线插件 For Nopcommerce V2.6 - wucf2004 支付宝插件 For Nopcommerce V2.6 - wucf2004 教你一招 - 如何给nopcommerce增加新闻类别模块 教你一招 - 如何使用中文包 教你一招 - 如何使用nopcommerce主题(v2.5) 教你一招 - 如何安装nopcommerce2.5 PropertyInfo PageLoad 事件执行两次 - wucf2004 js点击显示全部内容(用于内容比较长时) - wucf2004 - 博客园 asp.net解决中文乱码问题 - wucf2004 asp.net根据生日计算年龄(具体到年月天)
又一个不错的sql分页存储过程,支持排序、搜索
wucf2004 · 2008-11-14 · via 博客园 - wucf2004

CREATE PROCEDURE  sp_GetRecordByPageOrder

@tblName varchar(255)='wdf1', -- 表名

@fldName varchar(1000) = '*', -- 需要返回的列

@OrderfldName varchar(255)='userid', -- 排序的字段名

@PageSize int = 10, -- 页尺寸

@PageIndex int = 1, -- 页码

@IsReCount 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 @IsReCount != 0

begin

if @strWhere !=''

set @strSQL = 'select count(*) as Total from [' + @tblName + '] where '+@strWhere

else

set @strSQL = 'select count(*) as Total from [' + @tblName + ']'

end

--以上代码的意思是如果@IsReCount传递过来的不是0,就执行总数统计。以下的所有代码都是@IsReCount为0的情况

else

begin

if @OrderType != 0

begin

set @strTmp = '<(select min'

set @strOrder = ' order by [' + @OrderfldName +'] desc'

--如果@OrderType不是0,就执行降序,这句很重要!

end

else

begin

set @strTmp = '>(select max'

set @strOrder = ' order by [' + @OrderfldName +'] asc'

end

if @PageIndex = 1

begin

if @strWhere != ''

set @strSQL = 'select top ' + str(@PageSize) +' '+@fldName+ ' from [' + @tblName + '] where ' + @strWhere + ' ' + @strOrder

else

set @strSQL = 'select top ' + str(@PageSize) +' '+@fldName+ ' from ['+ @tblName + '] '+ @strOrder

--如果是第一页就执行以上代码,这样会加快执行速度

end

else

begin

--以下代码赋予了@strSQL以真正执行的SQL代码

set @strSQL = 'select top ' + str(@PageSize) +' '+@fldName+ ' from ['

+ @tblName + '] where [' + @OrderfldName + ']' + @strTmp + '(['+ @OrderfldName + ']) from (select top ' + str((@PageIndex-1)*@PageSize) + ' ['+ @OrderfldName + '] from [' + @tblName + ']' + @strOrder + ') as tblTmp)'+ @strOrder

if @strWhere != ''

set @strSQL = 'select top ' + str(@PageSize) +' '+@fldName+ ' from ['

+ @tblName + '] where [' + @OrderfldName + ']' + @strTmp + '(['

+ @OrderfldName + ']) from (select top ' + str((@PageIndex-1)*@PageSize) + ' ['

+ @OrderfldName + '] from [' + @tblName + '] where ' + @strWhere + ' '

+ @strOrder + ') as tblTmp) and ' + @strWhere + ' ' + @strOrder

end

end

exec (@strSQL)
GO