您好,登錄后才能下訂單哦!
這期內容當中小編將會給大家帶來有關SQL Server中如何使用row_number實現分頁,文章內容豐富且以專業的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
1、首先是
select ROW_NUMBER() over(order by id asc) as 'rowNumber', * from table1
生成帶序號的集合
2、再查詢該集合的 第 1 到第 5條數據
select * from (select ROW_NUMBER() over(order by id asc) as 'rowNumber', * from table1) as temp where rowNumber between 1 and 5
完整的Sql語句
declare @pagesize int; declare @pageindex int; set @pagesize = 3set @pageindex = 1; --第一頁select * from (select ROW_NUMBER() over(order by id asc) as 'rowNumber', * from table1) as temp where rowNumber between (((@pageindex-1)*@pagesize)+1) and (@pageindex*@pagesize)set @pageindex = 2; --第二頁select * from (select ROW_NUMBER() over(order by id asc) as 'rowNumber', * from table1) as temp where rowNumber between (((@pageindex-1)*@pagesize)+1) and (@pageindex*@pagesize)set @pageindex = 3; --第三頁select * from (select ROW_NUMBER() over(order by id asc) as 'rowNumber', * from table1) as temp where rowNumber between (((@pageindex-1)*@pagesize)+1) and (@pageindex*@pagesize)set @pageindex = 4;--第四頁select * from (select ROW_NUMBER() over(order by id asc) as 'rowNumber', * from table1) as temp where rowNumber between (((@pageindex-1)*@pagesize)+1) and (@pageindex*@pagesize)
下面我們來寫個存儲過程分頁
Alter Procedure PagePager @TableName varchar(80), @File varchar(1000),--- @Where varchar(500),---帶and連接 @OrderFile varchar(100), -- 排序字段 @OrderType varchar(10),--asc:順序,desc:倒序 @PageSize varchar(10), -- @PageIndex varchar(10) -- as if(ISNULL(@OrderFile, '') = '') begin set @OrderFile = 'ID'; end if(ISNULL(@OrderType,'') = '') begin set @OrderType = 'asc' end if(ISNULL(@File,'') = '') begin set @File = '*' end declare @select varchar(8000) set @select = ' select ' + @File + ' from ( select *,ROW_NUMBER() over(order by ' + @OrderFile + ' '+ @OrderType + ') as ''rowNumber'' from ' + @TableName + ' where 1=1 ' + @Where + ' ) temp where rowNumber between (((' + @PageIndex + ' - 1) * ' + @PageSize + ')+1) and (' + @PageIndex + '*'+ @PageSize+')' exec(@select)
上述就是小編為大家分享的SQL Server中如何使用row_number實現分頁了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關知識,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。