大数据量分页的存储过程(转,没有试验过)不过很好

来源:互联网 发布:php编译 编辑:程序博客网 时间:2024/04/30 12:33

create proc sp_PublicTurnPageWebSite(
 @TBName  nvarchar(100)='', --表名,如 pinyin
 @PageSize int=10,   --每页的记录数,默认为 10
 @CurPage int=1,   --表示当前页 1
 @KeyField nvarchar(100)='ID', --关键字段名,默认为 ID,该字段要求是表中的索引 或 无重复和不为空的字段
 @KeyAscDesc nvarchar(4)='ASC', --关键字的升、降序,默认为升序 ASC , 降序为 DESC
 @Fields  nvarchar(500)='*', --所选择的列名,默认为全选
 @Condition nvarchar(200)='', --where 条件,默认为空
 @Order  nvarchar(200)='' --排序条件,默认为空
) with encryption as
 if @TBName = ''
    begin
        raiserror('请指定表名!',11,1)
        return
    end
 if @PageSize <=0 or @CurPage <0 
    begin
        raiserror('当前页数和每页的记录数都必须大于零!',11,1)
        return
    end
 if @KeyAscDesc = 'DESC'
  set @KeyAscDesc = '<'
 else
  set @KeyAscDesc = '>'
 if @Condition <> ''
  set @Condition = ' where ' + @Condition
 declare @SQL nvarchar(2000)

 set @SQL = ''
 if @CurPage = 1
    set @SQL = @SQL + 'SELECT Top ' + cast(@PageSize as nvarchar(20)) + ' ' + @Fields + ' FROM ' + @TBName + @Condition + ' ' + @Order
 else
    begin
  declare @iTopNum int
  set @iTopNum = @PageSize * (@CurPage - 1)
  set @SQL = @SQL + 'declare @sLastValue nvarchar(100)' + char(13)
  set @SQL = @SQL + 'SELECT Top ' + cast(@iTopNum as nvarchar(20)) + ' @sLastValue=' + @KeyField + ' FROM ' + @TBName + @Condition + ' ' + @Order + char(13)
  
  declare @Condition2 nvarchar(200)
  if @Condition = ''
     set @Condition2 = ' where ' + @KeyField + @KeyAscDesc + '@sLastValue '
  else
     set @Condition2 = ' and ' + @KeyField + @KeyAscDesc + '@sLastValue '
  set @SQL = @SQL + 'SELECT Top ' + cast(@PageSize as nvarchar(20)) + ' ' + @Fields + ' FROM ' + @TBName + @Condition + @Condition2 + @Order
    end
 EXECUTE sp_executesql @SQL 

 

===========================================================================

下面的这个分页,项目开发中使用过

CREATE PROCEDURE pagination
@tblName varchar(2500) , -- 表名
@strGetFields varchar(1000) = '*', -- 需要返回的列
@fldName varchar(255)='', -- 排序的字段名
@PageSize int = 10, -- 页尺寸
@PageIndex int = 1, -- 页码
@doCount bit = 0, -- 返回记录总数, 非 0 值则返回
@OrderType bit = 0, -- 设置排序类型, 非 0 值则降序
@strWhere varchar(2500) = '' -- 查询条件 (注意: 不要加 where)

AS

declare @strSQL varchar(5000) -- 主语句
declare @strTmp varchar(110) -- 临时变量
declare @strOrder varchar(400) -- 排序类型
--declare @strfldName varchar(255) --对排序的字段名进行处理


--如果@doCount传递过来的不是0,就执行总数统计
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的情况
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 + "("+cast(substring(""+@fldName+"",Charindex(".",""+@fldName+"")+1,len(""+@fldName+"")-Charindex(".",""+@fldName+""))
as char)+") from (select top " + str((@PageIndex-1)*@PageSize) + " "
+ @fldName + " from " + @tblName + " where " + @strWhere + " "
+ @strOrder + ") as tblTmp) and " + @strWhere + " " + @strOrder

end

end

exec (@strSQL)

GO

原创粉丝点击