【收藏】SQL存储分页

--  作者:Fufu
--  发布时间:6/16/2008 11:20:56 AM
--  SqlServer2005的高效分页存储过程(支持多字段任意排序,不要求排序字段唯一)
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go


-- =============================================
-- Author:        <杨俊明>
-- Create date: <2006-11-05>
-- Description:    <高效分页存储过程,仅适用于Sql2005>
-- Notes:        <排序字段强烈建议建索引>
-- =============================================
Alter Procedure [dbo].[up_Page2005] 
 @TableName varchar(50),        --表名
 @Fields varchar(5000) = \'*\',    --字段名(全部字段为*)
 @OrderField varchar(5000),        --排序字段(必须!支持多字段)
 @sqlWhere varchar(5000) = Null,--条件语句(不用加where)
 @pageSize int,                    --每页多少条记录
 @pageIndex int = 1 ,            --指定当前为第几页
 @TotalPage int output            --返回总页数 
as
begin

    Begin Tran --开始事务

    Declare @sql nvarchar(4000);
    Declare @totalRecord int;    

    --计算总记录数
         
    if (@SqlWhere=\'\' or @sqlWhere=NULL)
        set @sql = \'select @totalRecord = count(*) from \' + @TableName
    else
        set @sql = \'select @totalRecord = count(*) from \' + @TableName + \' where \' + @sqlWhere

    EXEC sp_executesql @sql,N\'@totalRecord int OUTPUT\',@totalRecord OUTPUT--计算总记录数        
    
    --计算总页数
    select @TotalPage=CEILING((@totalRecord+0.0)/@PageSize)

    if (@SqlWhere=\'\' or @sqlWhere=NULL)
        set @sql = \'Select * FROM (select ROW_NUMBER() Over(order by \' + @OrderField + \') as rowId,\' + @Fields + \' from \' + @TableName 
    else
        set @sql = \'Select * FROM (select ROW_NUMBER() Over(order by \' + @OrderField + \') as rowId,\' + @Fields + \' from \' + @TableName + \' where \' + @SqlWhere    
        
    
    --处理页数超出范围情况
    if @PageIndex<=0 
        Set @pageIndex = 1
    
    if @pageIndex>@TotalPage
        Set @pageIndex = @TotalPage

     --处理开始点和结束点
    Declare @StartRecord int
    Declare @EndRecord int
    
    set @StartRecord = (@pageIndex-1)*@PageSize + 1
    set @EndRecord = @StartRecord + @pageSize - 1

    --继续合成sql语句
    set @Sql = @Sql + \') as \' + @TableName + \' where rowId between \' + Convert(varchar(50),@StartRecord) + \' and \' +  Convert(varchar(50),@EndRecord)
    
    Exec(@Sql)
    ---------------------------------------------------
    If @@Error <> 0
      Begin
        RollBack Tran
        Return -1
      End
     Else
      Begin
        Commit Tran
        Return @totalRecord ---返回记录总数
      End    
end

代码
exec usp_GetListByPage 'cms_article.id','cms_article inner join cms_catalog on

cms_catalog.id=cms_article.catalogid
','cms_article.id,cms_article.topic,cms_article.userid,cms_catalog.catalogname','cms_art

icle.vdatetime desc
','',2,100



CREATE proc usp_GetListByPage
(
@PKs nvarchar(100),
@Tables nvarchar(100),
@Fields nvarchar(500),
@Sort nvarchar(100),
@Where nvarchar(4000),
@CurrentPage int,
@PageSize int,
@RecordCount int=0 out
)
as
declare @SQL nvarchar(4000)
declare @SQLCount nvarchar(2000)
declare @Count int
select @SQL = 'select top ' + convert(nvarchar,@PageSize) + ' '+@Fields+' from ' + @Tables + ' where 1=1 '

if(rtrim(ltrim(@Where))!='')
begin
select @SQL = @SQL + ' and '+ @Where
end
select @SQL = @SQL +' and '+ @PKs +' not in'
select @SQL = @SQL + '(select top '+ convert(nvarchar,(@CurrentPage-1)*@PageSize) + ' ' + @PKs + ' from '+ @Tables +' where

1=1
'

if(rtrim(ltrim(@Where))!='')
begin
select @SQL = @SQL + ' and ' + @Where
end
--select @SQL = @SQL + ')'
if(rtrim(ltrim(@Sort))!='')
begin
select @SQL = @SQL + ' order by ' + @Sort + ')'
select @SQL = @SQL + ' order by ' + @Sort
end

select @SQLCount = 'select @RecordCount= count(*) from ' + @Tables + ' where 1=1'
if(rtrim(ltrim(@Where))!='')
begin
select @SQLCount = @SQLCount + ' and ' + @Where
end
print @SQLCount

EXEC sp_executesql @SQLCount,N'@RecordCount int out',@RecordCount out
print @RecordCount
--select @RecordCount = exec(@SQLCount)
print @SQL
exec(@SQL)





GO

 

posted @ 2010-03-23 09:47  chunchill  阅读(221)  评论(0)    收藏  举报