在sql server 2005 的新方法row_number做乙個查詢
selectrow_number()over (order by productid) as row,name from product
可以看到返回結果中每條記錄都有乙個唯一的表示其序列號的標誌。
例如我們在分頁中要獲取的6到第10條記錄就可以採用下面的方法
select row,name from
(select row_number() over(order by productid) as row,name from product)
as productswithrownumbers
where row >= 6 and row <=10
返回相關的記錄。
那我們就利用row_number() 做乙個儲存過程分頁
carate proc [dbo].[page_tbale1]
(@pageno int, --當前頁數值,最小為1,表示第一頁
@pagesize int, --每頁顯示條數
@pagecount int out,--獲取總頁數)as
set nocount on --使返回的結果中不包含有關受 transact-sql 語句影響的行數的資訊。--獲取總頁數
declare @itemscount int
select @pagecount =ceiling( (count(id)+0.0)/@pagesize)
from product
order by productid
if(@pageno <> 1)
begin
set @itemscount=(@pageno-1)*@pagesize+1
select row,name from
(select row_number() over(order by productid) as row,name from product)
as productswithrownumbers
whererow >= @itemscount and row <= @pageno * @pagesize
endelse
begin
select row,name from
(select row_number() over(order by productid) as row,name from product)
as productswithrownumbers
whererow >= 1 and row <= @pagesize
end
set rowcount 0
ROW NUMBER 真分頁 儲存過程
if exists select from sysobjects where name proc pages drop proc proc pages create proc proc pages tablename varchar 100 pagesize int,pageindex int,or...
分頁儲存過程 分頁儲存過程
分頁儲存過程 alter proc dbo p pageshow pagesize int,每頁大小 currentpage int out,當前頁 housename nvarchar 50 房產名稱 totalcount int out,總記錄數 totalpage int out 總頁數 as...
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 as...