当前位置:编程技术网 > 技术文章 > 简明扼要的掌握ado.net框架 > 文章详细内容

学习ado.net(6):sql分页

使用sql分页,作为一种轻量级的方式, 被广为流传。

很多很多的朋友都使用过一个叫做通用的分页存储过程,当然我在几个项目中也用到了这个技术,我们也自己设计了通用分页存储过程。比较多全面,拿出和大家分享(代码太长,还是把文件传上来大家下载好了):

create proc PgProc

@pageSize int = 10,      --页面大小

@curPage int = 1,         --当前页

@tbName varchar(50),  --表名,格式 books

@rtFiled varchar(100),  --返回字段,格式 bookname,bookimg,bookpic 或者 *

@idName varchar(50),  --索引字段,格式 id

@where varchar(50),     --条件表达式,格式 (id < 5 or name = 'aaa')

@sort varchar(50),        --排序方式,格式 order by id desc

@accuracy tinyint = 1,   --是否精确获取总页数:123三个等级。

--求总页数之前必须求总记录数字,总记录数有2中方式,

--一种是从系统表sysindexes获取(定时更新,不一定准确,且不支持查询条件)

--另一种就是count()函数,(精确数据,支持查询条件,但是在记录量大时候非常耗时,)

--由此出现如上三种不同的获取总页数方法

--     1、半精确:在有查询条件时候,使用count();无查询条件时候,使用系统表读取方法。【推荐】

--     2、不精确:在有无查询条件时候,都使用系统表读取方法。

--     3、完全精确:在有无查询条件时候,都使用count()函数。

@pageCount int output  --总页数

as

 

--查询记录总数模块 开始

declare @recordCount int      --记录总数

declare @tempSql Nvarchar(300)--临时存放sql语句

 

if (@accuracy = 1) -- 1 = 半精确

begin

       if (@where = '') --如果没有查询条件。

       begin            

              SELECT @recordCount = [rows]                                    --获取 记录总数

              FROM sysindexes

              WHERE (id = OBJECT_ID(@tbName)) AND (indid IN (0, 1))

       end

       else

       begin            

              set @tempSql = 'select @rc=count('+@idName+') from '+@tbName+' where '+ @where

              exec sp_executesql @tempSql,N'@rc int output',@recordCount output

       end

end

else

begin

       if (@accuracy = 3) -- 3 = 完全精确

       begin

              if (@where = '') --如果没有查询条件。

              begin            

                     set @tempSql = 'select @rc=count('+@idName+') from '+@tbName

                     exec sp_executesql @tempSql,N'@rc int output',@recordCount output

              end

              else

              begin            

                     set @tempSql = 'select @rc=count('+@idName+') from '+@tbName+' where '+ @where

                     exec sp_executesql @tempSql,N'@rc int output',@recordCount output

              end        

       end

       else                       -- 2 = 不精确

       begin

              SELECT @recordCount = [rows]                                    --获取 记录总数

              FROM sysindexes

              WHERE (id = OBJECT_ID(@tbName)) AND (indid IN (0, 1))

       end

end

--查询记录总数模块 结束

 

--计算 总页数

set @pageCount = @recordCount / @pageSize   

if (@recordCount % @pageSize <> 0)

       begin

              set @pageCount = @pageCount + 1

       end

 

print @pageCount

 

--特殊情况处理模块 开始

--模块目的:

--  提高特殊情况的查询效率

if (@curPage < 1) --如果当前页小于1,则返回空。

begin

       exec('select '+@rtFiled+' from '+@tbName+' where '+@idName+' is null')

       return

end

if (@curPage < 1) --如果当前页=1,则返回头开始条目。

begin

       exec('select '+@rtFiled+' from '+@tbName+' where '+@idName+' is null')

       return

end

if (@curPage > @pageCount) --如果当前页大于页面总数,则返回空。

begin

       exec('select '+@rtFiled+' from '+@tbName+' where '+@idName+' is null')

       return

end

--特殊情况处理模块 结束

 

declare @t int

set @t = @pageSize * (@curPage-1)

if (@where = '') --如果没有查询条件。

       begin

              exec('select top ' + @pageSize + ' '+@rtFiled+' from '+@tbName+' where '+@idName+'

       not in (select top '+@t+' '+@idName+' from '+@tbName+' '+ @sort +') ' + @sort)

              return

       end

else

       begin

              exec('select top ' + @pageSize + ' '+@rtFiled+' from '+@tbName+' where '+@idName+'

       not in (select top '+@t+' '+@idName+' from '+@tbName+' where '+@where+' '+ @sort +')

        and '+@where+' ' + @sort)

              return

       end

 

 

 

点击下载 作者|来源:原创(17fx.net)发表于:2009-1-1 21:30:48