程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 數據庫知識 >> SqlServer數據庫 >> 關於SqlServer >> sql server 2005分頁存儲過程和sql server 2000分頁存儲過程

sql server 2005分頁存儲過程和sql server 2000分頁存儲過程

編輯:關於SqlServer

SQL Server 2005分頁存儲過程和SQL Server 2000分頁存儲過程,sql 2005的分頁存儲過程分3個版本,一個是沒有優化過的,一個是優化過的,最後一個支持jion的,sql2000的分頁存儲過程,也可以運行在sql2005上,但是性能沒有sql2005的版本好。

USE [svnhost]

GO

/****** 對象:  StoredProcedure [dbo].[up_Page2005]    腳本日期: 05/21/2008 11:27:05 ******/

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

 

 


GO

/****** 對象:  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

時間:2009-08-29 Tag:存儲過程   分頁  

/****** 對象:  StoredProcedure [dbo].[up_Page2005V2_Join]    腳本日期: 05/21/2008 11:27:30 ******/

SET ANSI_NULLS ON

GO

SET QUOTED_IDENTIFIER ON

GO

-- =============================================

-- Author:        <Author,,Name>

-- Create date: <Create Date,,>

-- Description:    <Description,,>

-- =============================================

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

 

USE [game]

GO

/****** 對象:  StoredProcedure [dbo].[page]    腳本日期: 05/21/2008 11:37:12 ******/

SET ANSI_NULLS ON

GO

SET QUOTED_IDENTIFIER ON

GO


CREATE proc [dbo].[page]

@RecordCount int output,

@ReturnCount bit,

@QueryStr nvarchar(1000)='table1',--表名、視圖名、查詢語句

@PageSize int=20,  --每頁的大小(行數)

@PageCurrent int=2,  --要顯示的頁 從0開始

@FdShow nvarchar (2000)='*', --要顯示的字段列表

@IdentityStr nvarchar (100)='id', --主鍵

@WhereStr nvarchar (2000)='1=1',

@FdOrder nvarchar(100)='desc' --排序    只能取desc或者asc

as


set nocount on


declare


@sql nvarchar(2000)

 

if @WhereStr = '' begin

set @WhereStr = '1=1'

end


if @ReturnCount=1 begin

declare @tsql nvarchar(200)

set @tsql=N'select @RecordCount = count(*) from ' + @QueryStr + ' where ' + @WhereStr

exec sp_executesql @tsql,N'@RecordCount int output',@RecordCount output

end


if @PageCurrent = 0 begin

set @sql = 'select top ' + cast(@PageSize as nvarchar(3)) + ' ' + @FdShow + ' from ' + @QueryStr + ' where ' + @WhereStr + ' order by ' + @IdentityStr + ' ' + @FdOrder

end


else begin

if upper(@FdOrder) = 'DESC' begin

set @sql = 'select top ' + cast(@PageSize as nvarchar(3)) + ' ' + @FdShow + ' from ' + @QueryStr + ' where ' + @WhereStr + ' and ' + @IdentityStr + '< ( select min(' + @IdentityStr + ') from (select top ' + cast(@PageSize*@PageCurrent as nvarchar(10)) + ' ' + @IdentityStr + ' from ' + @QueryStr + ' where ' + @WhereStr + ' order by ' + @IdentityStr + ' desc) as t) order by ' + @IdentityStr + ' desc'

end

else begin

set @sql = 'select top ' + cast(@PageSize as nvarchar(3)) + ' ' + @FdShow + ' from ' + @QueryStr + ' where ' + @WhereStr + ' and ' + @IdentityStr + '> ( select max(' + @IdentityStr + ') from (select top ' + cast(@PageSize*@PageCurrent as nvarchar(10)) + ' ' + @IdentityStr + ' from ' + @QueryStr + ' where ' + @WhereStr + ' order by ' + @IdentityStr + ' asc) as t) order by ' + @IdentityStr + ' asc'

end

end

--print @sql

execute(@sql)

--select @t = datediff(ms,@t1,getdate())---------------------

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved