程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 數據庫知識 >> 其他數據庫知識 >> MSSQL >> SQL Server應用row_number分頁的完成辦法

SQL Server應用row_number分頁的完成辦法

編輯:MSSQL

SQL Server應用row_number分頁的完成辦法。本站提示廣大學習愛好者:(SQL Server應用row_number分頁的完成辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是SQL Server應用row_number分頁的完成辦法正文


本文為年夜家分享了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 = 3

set @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)

以上就是本文的全體內容,願望對年夜家進修row_number分頁有所贊助。

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