create procedure [dbo].[p_pagetest]
@sql nvarchar(max), --sql語句不包括排序
@curpage int, --當前頁
@pagerows int, --頁面尺寸
@order nvarchar(20), --排序字段
@ordertype nvarchar(10), --排序型別倒序desc或正序asc
@totalrecorder int output
asbegin
set nocount on;
declare @excesql nvarchar(4000)
--設定開始行號
declare @start_row_num as int
declare @end_row_num as int
if(@curpage=1)
begin
set @start_row_num = (@curpage - 1) * @pagerows
set @end_row_num = @start_row_num+@pagerows
endelse
begin
set @start_row_num = ((@curpage - 1) * @pagerows)+1
set @end_row_num = (@start_row_num+@pagerows)-1
end
--設定表示
declare @rownumber nvarchar(100)
set @rownumber = ', row_number() over(order by ' + @order +' '+ @ordertype + ') as rownumber from '
set @sql = replace(@sql,' from ',@rownumber)
--獲得總記錄數
set @excesql = 'with tmp as (' + @sql + ')
select @totalrecorder=max(rownumber) from tmp'
execute sp_executesql @excesql,n'@totalrecorder int output',@totalrecorder output
--查詢語句
set @excesql = 'with tmp as (' + @sql + ')
select * from tmp where rownumber between ' + convert(nvarchar,@start_row_num)
+ ' and ' + convert(nvarchar,@end_row_num)
execute(@excesql)
end
SQL 2005 多表分頁儲存過程
region 返回所有職位物件 帶分頁 region 返回所有職位物件帶分頁 當前頁 每頁顯示數 返回 頁數 返回總項數 物件集合 public static listgetallmodel int curpage,int size,ref int pagesize,ref int totalsiz...
SQL2005 分頁儲存過程
最近在使用sqlserver2005的資料庫編碼,查到有乙個row number order by 字段 的函式,通過該函式進行分頁非常的方便,請大家看看我寫的分頁儲存過程。分頁儲存過程 create procedure dbo proc testpage 表名 tablename nvarchar...
Sql2005中分頁儲存過程
set ansi nulls on goset ansi nulls on set quoted identifier on goalter procedure pagetest tblname varchar 255 表名 fldname varchar 255 主鍵欄位名 pagesize in...