SqlServer 2000、2005分页存储过程整理第1/3页

sql server 2005的分页存储过程分3个版本,一个是没有优化过的,一个是优化过的,最后一个支持join的,sql server 2000的分页存储过程,也可以运行在sql server 2005上,但是性能没有sql server 2005的版本好。

在最后 我还附带了一个二分法的分页存储过程,也很好用的说哈~~

1.SqlServer 2005:

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

CREATE proc [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 + ' with(nolock) 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 + ' with(nolock) 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 t where rowId between ' + Convert(varchar(50),@StartRecord) + ' and ' + Convert(varchar(50),@EndRecord)
   print @sql
  Exec(@Sql)
  ---------------------------------------------------
  If @@Error <> 0
   Begin
    RollBack Tran
    Return -1
   End
   Else
   Begin
    Commit Tran
    Return @totalRecord ---返回记录总数
   End
end

2.Sql Server 2005:

/****** 对象: StoredProcedure [dbo].[up_Page2005V2]  脚本日期: 05/21/2008 11:27:15 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:    <Author,,Name>
-- Create date: <Create Date,,>
-- Description:  <Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[up_Page2005V2]
  @TableName varchar(50),    --表名
 @Fields varchar(5000) = '*',  --字段名(全部字段为*)
 @OrderField varchar(5000),    --排序字段(必须!支持多字段)
 @sqlWhere varchar(5000) = Null,--条件语句(不用加where)
 @pageSize int,          --每页多少条记录
 @pageIndex int = 1 ,      --指定当前为第几页
 @totalRecord int = 0,
 @TotalPage int output      --返回总页数
AS
BEGIN

   Begin Tran --开始事务

  Declare @sql nvarchar(4000);

  if @totalRecord<=0 begin
    --计算总记录数

    if (@SqlWhere='' or @sqlWhere=NULL)
      set @sql = 'select @totalRecord = count(*) from ' + @TableName
    else
      set @sql = 'select @totalRecord = count(*) from ' + @TableName + ' with(nolock) where ' + @sqlWhere

    EXEC sp_executesql @sql,N'@totalRecord int OUTPUT',@totalRecord OUTPUT--计算总记录数
  end

  --计算总页数
  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 + ' with(nolock) 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 t where rowId between ' + Convert(varchar(50),@StartRecord) + ' and ' + Convert(varchar(50),@EndRecord)
   print @sql
  Exec(@Sql)
  ---------------------------------------------------
  If @@Error <> 0
   Begin
    RollBack Tran
    Return -1
   End
   Else
   Begin
    Commit Tran
    Return @totalRecord ---返回记录总数
   End
END

GO

3.Sql Server 2005:

/****** 对象: StoredProcedure [dbo].[up_Page2005V2_Join]  脚本日期: 05/21/2008 11:27:30 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[up_Page2005V2_Join]
  @TableName varchar(150),    --表名
 @Fields varchar(5000) = '*',  --字段名(全部字段为*)
 @OrderField varchar(5000),    --排序字段(必须!支持多字段)
 @sqlWhere varchar(5000) = Null,--条件语句(不用加where)
 @pageSize int,          --每页多少条记录
 @pageIndex int = 1 ,      --指定当前为第几页
 @totalRecord int = 0,
 @TotalPage int output      --返回总页数
AS
BEGIN

   Begin Tran --开始事务

  Declare @sql nvarchar(4000);

  if @totalRecord<=0 begin
    --计算总记录数

    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--计算总记录数
  end

  --计算总页数
  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 t where rowId between ' + Convert(varchar(50),@StartRecord) + ' and ' + Convert(varchar(50),@EndRecord)
   print @sql

  Exec(@Sql)
  ---------------------------------------------------
  If @@Error <> 0
   Begin
    RollBack Tran
    Return -1
   End
   Else
   Begin
    Commit Tran
    Return @totalRecord ---返回记录总数
   End
END

4.Sql Server 2000:

USE [game]
GO
/****** 对象: StoredProcedure [dbo].
                            123下一页阅读全文