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 首先是 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...
ROW NUMBER 實現分頁查詢
基本語法 select from select row number over order by 排序字段 desc asc as row,t.from role t where 條件 tt where tt.row between startindex and endindex 語法說明 row ...
row number 儲存過程分頁
在sql server 2005 的新方法row number做乙個查詢 selectrow number over order by productid as row,name from product 可以看到返回結果中每條記錄都有乙個唯一的表示其序列號的標誌。例如我們在分頁中要獲取的6到第10...