有返回值的分页

 
有返回值的分页.sql

declare @pageCount int
exec pgBook 8,190,'testdb1','*','id','','order by id asc',1,@pageCount
--exec pgBook 5,190,'testdb','*','id','id>500','order by id asc',1,@pageCount

select top 1 * from testdb


alter proc pagequery
@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,                --是否精确获取总页数:1、2、3三个等级。
--求总页数之前必须求总记录数字,总记录数有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
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值