------------------------------------
--用途:支援任意排序的分頁儲存過程
--說明:
------------------------------------
create procedure up_getrecordbypageorder
@tblname varchar(255), -- 表名
@fldname varchar(255), -- 顯示欄位名
@orderfldname varchar(255), -- 排序欄位名
@statfldname varchar(255), -- 統計欄位名
@pagesize int = 10, -- 頁尺寸
@pageindex int = 1, -- 頁碼
@isrecount bit = 0, -- 返回記錄總數, 非 0 值則返回
@ordertype bit = 0, -- 設定排序型別, 非 0 值則降序
@strwhere varchar(1000) = '' -- 查詢條件 (注意: 不要加 where)
as declare @strsql varchar(6000) -- 主語句
declare @strtmp varchar(100) -- 臨時變數
declare @strorder varchar(400) -- 排序型別
if @ordertype != 0
begin
set @strtmp = '<(select min'
set @strorder = ' order by [' + @orderfldname +'] desc'
end
else
begin
set @strtmp = '>(select max'
set @strorder = ' order by [' + @orderfldname +'] asc'
end
set @strsql = 'select top ' + str(@pagesize) + ' ' + @fldname + ' from ['
+ @tblname + '] where [' + @orderfldname + ']' + @strtmp + '(['
+ @orderfldname + ']) from (select top ' + str((@pageindex-1)*@pagesize) + ' ['
+ @orderfldname + '] from [' + @tblname + ']' + @strorder + ') as tbltmp)'
+ @strorder
if @strwhere != ''
set @strsql = 'select top ' + str(@pagesize) + ' ' + @fldname + ' from ['
+ @tblname + '] where [' + @orderfldname + ']' + @strtmp + '(['
+ @orderfldname + ']) from (select top ' + str((@pageindex-1)*@pagesize) + ' ['
+ @orderfldname + '] from [' + @tblname + '] where ' + @strwhere + ' '
+ @strorder + ') as tbltmp) and ' + @strwhere + ' ' + @strorder
if @pageindex = 1
begin
set @strtmp = ''
if @strwhere != ''
set @strtmp = ' where ' + @strwhere
set @strsql = 'select top ' + str(@pagesize) + ' ' + @fldname + ' from ['
+ @tblname + ']' + @strtmp + ' ' + @strorder
end
if @isrecount != 0
set @strsql = @strsql+' select count(1) as total from [' + @tblname + ']'
if @strwhere!=''
set @strsql = @strsql+' where ' + @strwhere
exec (@strsql)
go
分頁儲存過程 分頁儲存過程
分頁儲存過程 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...
分頁儲存過程
create procedure pro select pageindex int,pagesize int as select student.sno,student.sname,student.s grade.math,grade.physics,grade.huaxue,grade.chine...