分页存储过程,转载;原文http://www.cnblogs.com/chenqiang001/archive/2009/09/01/1558077.html

Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->CREATE PROCEDURE proc_pagination
(
@tblName nvarchar(1000), ----要显示的表或多个表的连接,必须参数
@fldName nvarchar(4000) = ‘*‘, ----要显示的字段列表
@fldSort nvarchar(4000) = null, ----排序字段列表或条件
@fldFSort nvarchar(4000) = null, ----反向排序字段列表或条件(这个需要在调用前准备好,且一定要与原始排序完全相反)
@strCondition nvarchar(4000) = null, ----查询条件,不需where
@ID nvarchar(1000), ----主表的主键,必须参数
@Dist bit = 0, ----是否添加查询字段的 DISTINCT 默认0不添加/1添加
@pageSize int = 10, ----每页显示的记录个数
@currentpage int = 1, ----要显示那一页的记录
@pageCount int = 1 output, ----查询结果分页后的总页数
@Counts int = 1 output ----查询到的记录数
)
AS
SET NOCOUNT ON
Declare @sqlTmp nvarchar(4000) ----存放动态生成的SQL语句
Declare @strTmp nvarchar(4000) ----存放取得查询结果总数的查询语句
Declare @strID nvarchar(1000) ----存放取得查询开头或结尾ID的查询语句

Declare @strSort nvarchar(4000) ----数据排序规则A
Declare @strFSort nvarchar(4000) ----数据排序规则B

Declare @SqlSelect nvarchar(50) ----对含有DISTINCT的查询进行SQL构造
Declare @SqlCounts nvarchar(50) ----对含有DISTINCT的总数查询进行SQL构造

Declare @IDSords nvarchar(4000) ----ID和所有参加排序的字段,用于distinct和order by

if @Dist = 0 --不带distinct
begin
set @SqlSelect = ‘select ‘
set @SqlCounts = ‘Count(1)‘
end
else
begin
set @SqlSelect = ‘select distinct ‘
set @SqlCounts = ‘Count(DISTINCT ‘[email protected]+‘)‘
end

--本来应该在这里分析@fldSort,然后自动生成反向排序字段
set @[email protected]
set @[email protected]

--------生成查询语句--------
--此处@strTmp为取得查询结果数量的语句
if @strCondition is null or @strCondition=‘‘ --没有查询条件,不带where
begin
set @sqlTmp = @fldName + ‘ From ‘ + @tblName
set @strTmp = @SqlSelect+‘ @Counts=‘[email protected]+‘ FROM ‘[email protected]
set @strID = ‘ From ‘ + @tblName
end
else
begin
set @sqlTmp = + @fldName + ‘ From ‘ + @tblName + ‘ where ‘ + @strCondition
set @strTmp = @SqlSelect+‘ @Counts=‘[email protected]+‘ FROM ‘[email protected] + ‘ where ‘ + @strCondition
set @strID = ‘ From ‘ + @tblName + ‘ where ‘ + @strCondition
end

----取得查询结果总数量-----
exec sp_executesql @strTmp,N‘@Counts int out ‘,@Counts out

declare @tmpCounts int
if @Counts = 0
set @tmpCounts = 1
else
set @tmpCounts = @Counts

--取得分页总数
set @pageCount=(@[email protected])/@pageSize

/**//**当前页大于总页数 取最后一页**/
if @currentpage>@pageCount
set @[email protected]

--/*-----数据分页2分处理-------*/
declare @pageIndex int --总数/页大小
declare @lastcount int --总数%页大小

set @pageIndex = @tmpCounts/@pageSize
set @lastcount = @tmpCounts%@pageSize
if @lastcount > 0
set @pageIndex = @pageIndex + 1 [email protected]>0说明最后一页还有记录,所以就应该再多一页
else
set @lastcount = @pageSize

--//***显示分页
if @strCondition is null or @strCondition=‘‘ --没有查询条件where
begin
if @pageIndex<2 or @currentpage<[email protected] / 2 + @pageIndex % 2 --前半部分数据处理
begin
if @fldSort is null or @fldSort=‘‘ --没有排序
set @[email protected]+‘ top ‘+ CAST(@pageSize as VARCHAR(4))+‘ ‘+ @fldName+‘ from ‘[email protected]
+‘ where ‘[email protected]+‘ not in(select ‘[email protected]+‘ from ‘
+‘(‘+ @SqlSelect+‘ top ‘+ CAST(@pageSize*(@currentpage-1) as Varchar(20))
+‘ ‘+ @fldName +‘ from ‘[email protected] +‘) as TempTBL)‘
--取的是id不在前(@currentpage-1)页中,即不是前(@currentpage-1)*@pagesize条记录的前@pagesize条记录
else --如果有排序的话
set @[email protected]+‘ top ‘+ CAST(@pageSize as VARCHAR(4))+‘ ‘+ @fldName+‘ from ‘[email protected]
+‘ where ‘[email protected]+‘ not in(select ‘[email protected]+‘ from (‘
+ @SqlSelect+‘ top ‘+ CAST(@pageSize*(@currentpage-1) as Varchar(20)) +‘ ‘+ @fldName
+‘ from ‘[email protected]
+‘ order by ‘+ @strSort+‘) as TempTBL)‘
+‘ order by ‘+ @strSort
--按正序排序取前(@currentpage-1)*@pagesize条记录,然后再按正序排序把前(@currentpage-1)*@pagesize条记录排除取前@pagesize条记录
end
else --后半部分数据处理
begin
set @currentpage = @[email protected]+1
if @currentpage <= 1 --最后一页数据显示
if @fldSort is null or @fldSort=‘‘ --没有排序
set @[email protected]+‘ ‘[email protected]+‘ from (‘
+ @SqlSelect +‘ ‘ + @fldName+ ‘ from ‘[email protected]
+‘ where ‘[email protected] + ‘ not in (select ‘[email protected]+‘ from(‘
[email protected]+‘ top ‘+CAST(@pageSize*(@pageIndex-1) as VARCHAR(4))+‘ ‘[email protected]
+‘ from ‘[email protected]
+‘) as TempTBL)) AS TempTB‘
--如果是最后一页就没必要再用top了,直接把不在前@pagesize*(@pageindex-1)页中的记录取出就可以
else --如果有排序的话
set @[email protected]+‘ ‘[email protected]+‘ from (‘
[email protected]+‘ top ‘+ CAST(@lastcount as VARCHAR(4))+‘ ‘+ @fldName
+‘ from ‘[email protected]
+‘ order by ‘+ @strFSort +‘) AS TempTB‘+‘ order by ‘+ @strSort
--先反向排序,取出前@lastcount条记录,再进行正向排序
else --不是最后一页,即后半部分的其他页
if @fldSort is null or @fldSort=‘‘ --没有排序时
set @[email protected]+‘ top ‘+ CAST(@pageSize as VARCHAR(4))+‘ ‘+ @fldName+‘ from ‘[email protected]
+‘ where ‘[email protected]+‘ not in(select ‘[email protected]+‘ from(‘
+ @SqlSelect+‘ top ‘+ CAST(@pageSize*(@currentpage-1) as Varchar(20))
+‘ ‘+ @fldName +‘ from ‘[email protected] +‘) as TempTBL)‘
--不排序时跟前半部分应该一样处理
/*
set @[email protected]+‘ ‘[email protected]+‘ from (‘
[email protected]+‘ top ‘+ CAST(@pageSize as VARCHAR(4))+‘ ‘+ @fldName
+‘ from ‘[email protected] +‘ where ‘[email protected]+‘ not in(‘
+ @SqlSelect+‘ top ‘+ CAST(@pageSize*(@currentpage-2)[email protected] as Varchar(20)) +‘ ‘+ @fldName
+‘ from ‘[email protected] +‘)‘
+‘) AS TempTB‘
*/
else --有排序时
set @[email protected]+‘ ‘[email protected]+‘ from (‘
[email protected]+‘ top ‘+ CAST(@pageSize as VARCHAR(4))+‘ ‘+ @fldName
+‘ from ‘[email protected] +‘ where ‘[email protected]+‘ not in(select ‘[email protected]+‘ from(‘
+ @SqlSelect+‘ top ‘+ CAST(@pageSize*(@currentpage-2)[email protected] as Varchar(20)) +‘ ‘+ @fldName
+‘ from ‘[email protected] +‘ order by ‘+ @strFSort +‘) as TempTBL)‘
+‘ order by ‘+ @strFSort +‘) AS TempTB‘
+‘ order by ‘+ @strSort
--先反向排序取出前(@currentpage-1)页的记录(其中包括不满一整页的最后一页,所以是前@pagesize*(@currentpage-2)[email protected]条记录)
--然后仍然反向排序,这回取不在上一结果集里的前@pagesize条记录,即该得到的记录
--最后进行一次正向排序
end
end
else --有查询条件
begin
if @pageIndex<2 or @currentpage<[email protected] / 2 + @pageIndex % 2 --前半部分数据处理
begin
if @fldSort is null or @fldSort=‘‘ --没有排序
set @[email protected]+‘ top ‘+ CAST(@pageSize as VARCHAR(4))+‘ ‘+ @fldName +‘ from ‘[email protected]
+‘ where ‘[email protected]+‘ not in(select ‘[email protected]+‘ from(‘
+ @SqlSelect+‘ top ‘+ CAST(@pageSize*(@currentpage-1) as Varchar(20)) +‘ ‘+ @fldName
+‘ from ‘[email protected]
+‘ Where ‘ + @strCondition + ‘) as TempTBL)‘
+‘ and ‘ + @strCondition
--先取出带where后的前@pagesize*(@currentpage-1)条记录
--再取出不在这@pagesize*(@currentpage-1)条记录中的还where的前@pagesize条记录
else --有排序
set @[email protected]+‘ top ‘+ CAST(@pageSize as VARCHAR(4))+‘ ‘+ @fldName +‘ from ‘[email protected]
+‘ where ‘[email protected]+‘ not in(select ‘[email protected]+‘ from(‘
+ @SqlSelect+‘ top ‘+ CAST(@pageSize*(@currentpage-1) as Varchar(20)) +‘ ‘+ @fldName
+‘ from ‘[email protected] +‘ Where ‘ + @strCondition + ‘ order by ‘+ @strSort +‘) as TempTBL)‘
+‘ and ‘ + @strCondition + ‘ order by ‘+ @strSort
--1、正向排序,取出带where后的前@pagesize*(@currentpage-1)条记录
--2、正向排序,取出不在这@pagesize*(@currentpage-1)条记录中的还where的前@pagesize条记录
end
else --后半部分
begin
set @currentpage = @[email protected]+1 --后半部分数据处理
if @currentpage <= 1 --最后一页数据显示
if @fldSort is null or @fldSort=‘‘
set @[email protected]+‘ ‘[email protected] + ‘ from ‘[email protected]
+‘ where ‘[email protected] + ‘ not in (select ‘[email protected]+‘ from(‘
[email protected]+‘ top ‘+CAST(@pagesize*(@pageIndex-1) as VARCHAR(4))+‘ ‘[email protected]
+‘ from ‘[email protected] +‘ where ‘[email protected]
+‘) as TempTBL) and ‘[email protected]
--取不在前@pageIndex-1页中的记录
else --有排序,即既有条件又有排序
set @[email protected]+‘ ‘[email protected]+‘ from (‘
[email protected]+‘ top ‘+ CAST(@lastcount as VARCHAR(4))+‘ ‘+ @fldName
+‘ from ‘[email protected]+‘ where ‘+ @strCondition
+‘ order by ‘+ @strFSort +‘) AS TempTB‘+‘ order by ‘+ @strSort
--先倒序排列,取前@lastcount条记录
--再正序排序
else --后半部分,但不是最后一页
if @fldSort is null or @fldSort=‘‘ --没有排序条件
set @[email protected]+‘ top ‘+ CAST(@pageSize as VARCHAR(4))+‘ ‘+ @fldName +‘ from ‘[email protected]
+‘ where ‘[email protected]+‘ not in(select ‘[email protected]+‘ from(‘
+ @SqlSelect+‘ top ‘+ CAST(@pageSize*(@currentpage-1) as Varchar(20)) +‘ ‘+ @fldName
+‘ from ‘[email protected]
+‘ Where ‘ + @strCondition + ‘) as TempTBL)‘
+‘ and ‘ + @strCondition
--不排序时应该与前半部分相同处理
else --有排序字段、有查询条件、后半部分非最末页
set @[email protected]+‘ ‘[email protected]+‘ from (‘
[email protected]+‘ top ‘+ CAST(@pageSize as VARCHAR(4))+‘ ‘+ @fldName
+‘ from ‘[email protected] +‘ where ‘[email protected]+‘ not in (‘
+‘select ‘[email protected]+‘ from (‘
+ @SqlSelect+‘ top ‘+ CAST(@pageSize*(@currentpage-2)[email protected] as Varchar(20)) +‘ ‘+ @fldName
+‘ from ‘[email protected] +‘ where ‘+ @strCondition +‘ order by ‘+ @strFSort +‘) as TempTBL)‘
+‘ and ‘ + @strCondition +‘ order by ‘+ @fldFSort +‘) AS TempTB‘+‘ order by ‘+ @strSort
end
end

------返回查询结果-----
exec sp_executesql @strTmp
--print @strTmp
SET NOCOUNT OFF

时间: 2024-08-11 01:20:04

分页存储过程,转载;原文http://www.cnblogs.com/chenqiang001/archive/2009/09/01/1558077.html的相关文章

java中volatile关键字的含义(转载自http://www.cnblogs.com/aigongsi/archive/2012/04/01/2429166.html)

在java线程并发处理中,有一个关键字volatile的使用目前存在很大的混淆,以为使用这个关键字,在进行多线程并发处理的时候就可以万事大吉. Java语言是支持多线程的,为了解决线程并发的问题,在语言内部引入了 同步块 和 volatile 关键字机制. synchronized 同步块大家都比较熟悉,通过 synchronized 关键字来实现,所有加上synchronized 和 块语句,在多线程访问的时候,同一时刻只能有一个线程能够用 synchronized 修饰的方法 或者 代码块.

C++了解free和delete(转自:http://www.cnblogs.com/mrye/archive/2012/09/01/2667079.html)

void MyMethod1() {     using namespace std;     int a=6;     int b=6;     int* pa=new int;     int* pb=new int;     *pa=a;     pb=pa;     cout<<"pa的内容赋值为:"<<a<<endl;     delete(pa);     //free(pa);//加上这句造成pa不可用,     //cout<&

java反射详解 (转至 http://www.cnblogs.com/rollenholt/archive/2011/09/02/2163758.html)

本篇文章依旧采用小例子来说明,因为我始终觉的,案例驱动是最好的,要不然只看理论的话,看了也不懂,不过建议大家在看完文章之后,在回过头去看看理论,会有更好的理解. 下面开始正文. [案例1]通过一个对象获得完整的包名和类名 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 package Reflect; /**  * 通过一个对象获得完整的包名和类名  * */ class Demo{     //other codes... } class hello{     pu

tomcat通过conf-Catalina-localhost目录发布项目详解 摘自:http://www.cnblogs.com/iyangyuan/archive/2013/09/12/3316444.html

Tomcat发布项目的方式大致有三种,但小菜认为通过在tomcat的conf/Catalina/localhost目录下添加配置文件,来发布项目,是最佳选择. 因为这样对tomcat的入侵性最小,只需要新增一个配置文件,不需要修改原有配置:而且支持动态解析,修改完代码直接生效(修改配置除外). 但是网上关于这种方法的介绍很简单,小菜来补充一下. 1.直接在eclipse中添加一个server,添加过程中指明tomcat的路径即可. 2.在tomcat服务器的conf\Catalina\local

sql 分页(原:http://www.cnblogs.com/fly_zj/archive/2010/07/06/1772536.html)

SQLSERVER 2005分页脚本性能实测 网上有很多的分页T-SQL代码,分散在各处,主要的问题是:测试时数据量太小,最多只有2万多条,不同方法的体现出性能差别有疑惑,另外当初在学习sqlserver 2005 时,一位同学信誓旦旦说分页 在SQLSERVER 2005中可以使用EXCEPT关键字,性能最好,理由是EXCEPT是集合运算.当时信以为真.工作以后,发现在SQLSERVER 2005中的分页存储过程都没有用到EXCEPT方法,就更疑惑了. 这次系统的看<Inside Micros

static作用(修饰函数、局部变量、全局变量)转自http://www.cnblogs.com/stoneJin/archive/2011/09/21/2183313.html

static作用(修饰函数.局部变量.全局变量) 在C语言中,static的字面意思很容易把我们导入歧途,其实它的作用有三条. (1)先来介绍它的第一条也是最重要的一条:隐藏. 当我们同时编译多个文件时,所有未加static前缀的全局变量和函数都具有全局可见性.为理解这句话,我举例来说明.我们要同时编译两个源文件,一个是a.c,另一个是main.c. 下面是a.c的内容 char a = 'A'; // global variablevoid msg() {    printf("Hello\n

距离计算方法总结 转自http://www.cnblogs.com/xbinworld/archive/2012/09/24/2700572.html#2663469

在做分类时常常需要估算不同样本之间的相似性度量(Similarity Measurement),这时通常采用的方法就是计算样本间的“距离”(Distance).采用什么样的方法计算距离是很讲究,甚至关系到分类的正确与否. 本文的目的就是对常用的相似性度量作一个总结. 本文目录: 1. 欧氏距离 2. 曼哈顿距离 3. 切比雪夫距离 4. 闵可夫斯基距离 5. 标准化欧氏距离 6. 马氏距离 7. 夹角余弦 8. 汉明距离 9. 杰卡德距离 & 杰卡德相似系数 10. 相关系数 & 相关距离

大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)

在项目中,我们经常遇到或用到分页,那么在大数据量(百万级以上)下,哪种分页算法效率最优呢?我们不妨用事实说话. 测试环境 硬件:CPU 酷睿双核T5750  内存:2G 软件:Windows server 2003    +   Sql server 2005 OK,我们首先创建一数据库:data_Test,并在此数据库中创建一表:tb_TestTable 1create database data_Test  --创建数据库data_Test  2GO  3use data_Test  4GO

分页存储过程[转载]

SQL2000CREATE PROCEDURE [dbo].[ProcCustomPage] ( @Table_Name varchar(5000), --表名 @Sign_Record varchar(50), --主键 @Filter_Condition varchar(1000), --筛选条件,不带where @Page_Size int, --页大小 @Page_Index int, --页索引 @TaxisField varchar(1000), --排序字段 @Taxis_Sign