SqlServer 2000、2005分頁(yè)存儲(chǔ)過(guò)程整理
來(lái)源:易賢網(wǎng) 閱讀:1080 次 日期:2015-03-03 10:11:19
溫馨提示:易賢網(wǎng)小編為您整理了“SqlServer 2000、2005分頁(yè)存儲(chǔ)過(guò)程整理”,方便廣大網(wǎng)友查閱!

sql server 2005的分頁(yè)存儲(chǔ)過(guò)程分3個(gè)版本,一個(gè)是沒(méi)有優(yōu)化過(guò)的,一個(gè)是優(yōu)化過(guò)的,最后一個(gè)支持join的,sql server 2000的分頁(yè)存儲(chǔ)過(guò)程,也可以運(yùn)行在sql server 2005上,但是性能沒(méi)有sql server 2005的版本好。

在最后 我還附帶了一個(gè)二分法的分頁(yè)存儲(chǔ)過(guò)程,也很好用的說(shuō)哈~~

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,--條件語(yǔ)句(不用加where)

@pageSize int, --每頁(yè)多少條記錄

@pageIndex int = 1 , --指定當(dāng)前為第幾頁(yè)

@TotalPage int output --返回總頁(yè)數(shù)

as

begin

Begin Tran --開(kāi)始事務(wù)

Declare @sql nvarchar(4000);

Declare @totalRecord int;

--計(jì)算總記錄數(shù)

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--計(jì)算總記錄數(shù)

--計(jì)算總頁(yè)數(shù)

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

--處理頁(yè)數(shù)超出范圍情況

if @PageIndex<=0

Set @pageIndex = 1

if @pageIndex>@TotalPage

Set @pageIndex = @TotalPage

--處理開(kāi)始點(diǎn)和結(jié)束點(diǎn)

Declare @StartRecord int

Declare @EndRecord int

set @StartRecord = (@pageIndex-1)*@PageSize + 1

set @EndRecord = @StartRecord + @pageSize - 1

--繼續(xù)合成sql語(yǔ)句

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 ---返回記錄總數(shù)

End

end

2.Sql Server 2005:

/****** 對(duì)象: 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,--條件語(yǔ)句(不用加where)

@pageSize int, --每頁(yè)多少條記錄

@pageIndex int = 1 , --指定當(dāng)前為第幾頁(yè)

@totalRecord int = 0,

@TotalPage int output --返回總頁(yè)數(shù)

AS

BEGIN

Begin Tran --開(kāi)始事務(wù)

Declare @sql nvarchar(4000);

if @totalRecord<=0 begin

--計(jì)算總記錄數(shù)

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--計(jì)算總記錄數(shù)

end

--計(jì)算總頁(yè)數(shù)

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

--處理頁(yè)數(shù)超出范圍情況

if @PageIndex<=0

Set @pageIndex = 1

if @pageIndex>@TotalPage

Set @pageIndex = @TotalPage

--處理開(kāi)始點(diǎn)和結(jié)束點(diǎn)

Declare @StartRecord int

Declare @EndRecord int

set @StartRecord = (@pageIndex-1)*@PageSize + 1

set @EndRecord = @StartRecord + @pageSize - 1

--繼續(xù)合成sql語(yǔ)句

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 ---返回記錄總數(shù)

End

END

GO

3.Sql Server 2005:

/****** 對(duì)象: 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,--條件語(yǔ)句(不用加where)

@pageSize int, --每頁(yè)多少條記錄

@pageIndex int = 1 , --指定當(dāng)前為第幾頁(yè)

@totalRecord int = 0,

@TotalPage int output --返回總頁(yè)數(shù)

AS

BEGIN

Begin Tran --開(kāi)始事務(wù)

Declare @sql nvarchar(4000);

if @totalRecord<=0 begin

--計(jì)算總記錄數(shù)

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--計(jì)算總記錄數(shù)

end

--計(jì)算總頁(yè)數(shù)

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

--處理頁(yè)數(shù)超出范圍情況

if @PageIndex<=0

Set @pageIndex = 1

if @pageIndex>@TotalPage

Set @pageIndex = @TotalPage

--處理開(kāi)始點(diǎn)和結(jié)束點(diǎn)

Declare @StartRecord int

Declare @EndRecord int

set @StartRecord = (@pageIndex-1)*@PageSize + 1

set @EndRecord = @StartRecord + @pageSize - 1

--繼續(xù)合成sql語(yǔ)句

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 ---返回記錄總數(shù)

End

END

4.Sql Server 2000:

USE [game]

GO

/****** 對(duì)象: StoredProcedure [dbo].

更多信息請(qǐng)查看IT技術(shù)專欄

更多信息請(qǐng)查看數(shù)據(jù)庫(kù)
易賢網(wǎng)手機(jī)網(wǎng)站地址:SqlServer 2000、2005分頁(yè)存儲(chǔ)過(guò)程整理
由于各方面情況的不斷調(diào)整與變化,易賢網(wǎng)提供的所有考試信息和咨詢回復(fù)僅供參考,敬請(qǐng)考生以權(quán)威部門公布的正式信息和咨詢?yōu)闇?zhǔn)!

2026國(guó)考·省考課程試聽(tīng)報(bào)名

  • 報(bào)班類型
  • 姓名
  • 手機(jī)號(hào)
  • 驗(yàn)證碼
關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡(jiǎn)要咨詢 | 簡(jiǎn)要咨詢須知 | 新媒體/短視頻平臺(tái) | 手機(jī)站點(diǎn) | 投訴建議
工業(yè)和信息化部備案號(hào):滇ICP備2023014141號(hào)-1 云南省教育廳備案號(hào):云教ICP備0901021 滇公網(wǎng)安備53010202001879號(hào) 人力資源服務(wù)許可證:(云)人服證字(2023)第0102001523號(hào)
云南網(wǎng)警備案專用圖標(biāo)
聯(lián)系電話:0871-65099533/13759567129 獲取招聘考試信息及咨詢關(guān)注公眾號(hào):hfpxwx
咨詢QQ:1093837350(9:00—18:00)版權(quán)所有:易賢網(wǎng)
云南網(wǎng)警報(bào)警專用圖標(biāo)