使用sql2005的新特性分页的储存过程:Top,Row_Number
生活随笔
收集整理的這篇文章主要介紹了
使用sql2005的新特性分页的储存过程:Top,Row_Number
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
GO
/******?對象:??StoredProcedure?[dbo].[uspCustomPaging]??? ******/
SET?ANSI_NULLS?ON
GO
SET?QUOTED_IDENTIFIER?ON
GO
Create procedure?[dbo].[uspCustomPaging]?
????@TableName?varchar(50),????????????????? --表或視圖名
????@Fields?varchar(5000)?=?'*',????????????? --字段名(全部字段為*)
????@OrderFields?varchar(5000),????????????--排序字段(必須!支持多字段,建議建索引)
????@SqlWhere?varchar(5000)?=?'',??????? ?--條件語句(如and?Name='a')
????@PageSize?int,??????????????????????????????????? ?--每頁多少條記錄
????@PageIndex?int?=?1?,?????????????????????????? --指定當前為第幾頁
????@TotalPages?int?output??????????????????? --返回總頁數?
as
begin
????declare?@sql?nvarchar(4000)
????declare?@TotalRecords?int???
????--計算總記錄數及總頁數?????
????set?@sql?=?'select?@TotalRecords?=?count(*)?from?'?+?@TableName?+?'?where?1=1?'?+?@sqlWhere
????exec?sp_executesql?@sql,N'@totalRecords?int?output',@TotalRecords?output
????select?@TotalPages=CEILING((@TotalRecords+0.0)/@PageSize)
????--處理頁數超出范圍情況
????if?@PageIndex<=0?
????????set?@PageIndex?=?1
????if?@PageIndex>@TotalPages
????????set?@PageIndex?=?@TotalPages
????set?@sql?=?'select?'+?@Fields?+?'?from?(select?top(@PageIndex*@PageSize)?'?+?@Fields?+?',row_number()?over(order?by?'?+?@OrderFields?+?')?as?rowNumber?from?'?+?@TableName?+?'?where?1=1?'?+?@SqlWhere?+?')?t?where?t.rowNumber?>=?((@PageIndex-1)*@PageSize+1)'
????
????--print?@Sql???
????exec?sp_executesql?@sql,N'@PageIndex?int,?@PageSize?int',@PageIndex,@PageSize??
end
/******?對象:??StoredProcedure?[dbo].[uspCustomPaging]??? ******/
SET?ANSI_NULLS?ON
GO
SET?QUOTED_IDENTIFIER?ON
GO
Create procedure?[dbo].[uspCustomPaging]?
????@TableName?varchar(50),????????????????? --表或視圖名
????@Fields?varchar(5000)?=?'*',????????????? --字段名(全部字段為*)
????@OrderFields?varchar(5000),????????????--排序字段(必須!支持多字段,建議建索引)
????@SqlWhere?varchar(5000)?=?'',??????? ?--條件語句(如and?Name='a')
????@PageSize?int,??????????????????????????????????? ?--每頁多少條記錄
????@PageIndex?int?=?1?,?????????????????????????? --指定當前為第幾頁
????@TotalPages?int?output??????????????????? --返回總頁數?
as
begin
????declare?@sql?nvarchar(4000)
????declare?@TotalRecords?int???
????--計算總記錄數及總頁數?????
????set?@sql?=?'select?@TotalRecords?=?count(*)?from?'?+?@TableName?+?'?where?1=1?'?+?@sqlWhere
????exec?sp_executesql?@sql,N'@totalRecords?int?output',@TotalRecords?output
????select?@TotalPages=CEILING((@TotalRecords+0.0)/@PageSize)
????--處理頁數超出范圍情況
????if?@PageIndex<=0?
????????set?@PageIndex?=?1
????if?@PageIndex>@TotalPages
????????set?@PageIndex?=?@TotalPages
????set?@sql?=?'select?'+?@Fields?+?'?from?(select?top(@PageIndex*@PageSize)?'?+?@Fields?+?',row_number()?over(order?by?'?+?@OrderFields?+?')?as?rowNumber?from?'?+?@TableName?+?'?where?1=1?'?+?@SqlWhere?+?')?t?where?t.rowNumber?>=?((@PageIndex-1)*@PageSize+1)'
????
????--print?@Sql???
????exec?sp_executesql?@sql,N'@PageIndex?int,?@PageSize?int',@PageIndex,@PageSize??
end
轉載于:https://www.cnblogs.com/jordan2009/archive/2012/06/07/2539114.html
總結
以上是生活随笔為你收集整理的使用sql2005的新特性分页的储存过程:Top,Row_Number的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Linq 左连接 内连接
- 下一篇: 写个随笔解解闷-书签漫游