create procedure [dbo].[sp_page]
(
@sql nvarchar(1024), --查詢語句
@sort nvarchar(100) = '', --排序字段
@pagesize int = 20, --分頁大小
@pageindex int = 1, --分頁索引
@totalcount int = 0 output --總數
)
as-- 值預設值
if (isnull(@pagesize,0)=0)
set @pagesize=20
if (isnull(@pageindex,0)=0)
set @pageindex=1
set nocount on
/*宣告查詢字串*/
declare @strsql nvarchar(4000)
set @strsql = ' select @totalcount=count(*) from ('+@sql+') as t '
/*取得查詢結果總數*/
exec sp_executesql
@strsql,
n'@totalcount int=0 output',
@totalcount=@totalcount output
declare @itemcount int
declare @_pageindex int
set @_pageindex = @pageindex; --索引從1開始
--set @_pageindex = @pageindex + 1; --索引從0開始
/*確定搜尋邊界*/
set @itemcount = @totalcount - @pagesize * @_pageindex
if(@itemcount < 0)
set @itemcount = @itemcount + @pagesize
else
set @itemcount = @pagesize
if(@itemcount < 0) return 1
if(@sort != '')
begin
/*宣告排序變數*/
declare @indexsort1 nvarchar(50), @indexsort2 nvarchar(50), @sort1 nvarchar(50), @sort2 nvarchar(50)
set @sort1 = @sort
set @sort2 = replace(replace(replace(@sort, 'desc', '@sort'), 'asc', 'desc'), '@sort', 'asc')
set @strsql = 'select * from
(select top ' + str(@itemcount) + ' * from
(select top ' + str(@pagesize * @_pageindex) + ' * from
('+@sql+') as t0
order by '+@sort1 +') as t1
order by '+@sort2 +') as t2
order by ' +@sort
end
else
begin
set @strsql = 'select * from
(select top ' + str(@itemcount) + ' * from
(select top ' + str(@pagesize * @_pageindex) + ' * from
('+@sql+') as t0)
as t1)
as t2'
endexec sp_executesql
@strsql
Repeater呼叫儲存過程 數字分頁
default.aspx.cs using system using system.data using system.configuration using system.web using system.collections using system.web.security using sy...
分頁儲存過程 分頁儲存過程
分頁儲存過程 alter proc dbo p pageshow pagesize int,每頁大小 currentpage int out,當前頁 housename nvarchar 50 房產名稱 totalcount int out,總記錄數 totalpage int out 總頁數 as...
分頁儲存過程
create proc p sobigo percentpage tblname varchar 255 t category 表名 strgetfields varchar 1000 需要返回的列 fldname varchar 255 排序的欄位名 pagesize int 10,頁尺寸 pag...