create procedure getrecordfrompage
@tblname varchar(255), -- 表名
@fldname varchar(255), -- 欄位名
@pagesize int = 10, -- 頁尺寸
@pageindex int = 1, -- 頁碼
@ordertype bit = 0, -- 設定排序型別, 非 0 值則降序
@strwhere varchar(2000) = '' -- 查詢條件 (注意: 不要加 where)
asdeclare @strsql varchar(6000) -- 主語句
declare @strtmp varchar(1000) -- 臨時變數
declare @strorder varchar(500) -- 排序型別
if @ordertype != 0 --降序
begin
set @strtmp = '<(select min'
set @strorder = ' order by [' + @fldname + '] desc'
endelse --公升序
begin
set @strtmp = '>(select max'
set @strorder = ' order by [' + @fldname +'] asc'
endif @pageindex = 1
begin
set @strtmp = ''
if @strwhere != ''
set @strtmp = ' where (' + @strwhere + ')'
set @strsql = 'select top ' + str(@pagesize) + ' * from ['
+ @tblname + ']' + @strtmp + ' ' + @strorder
endelse begin
if @strwhere != ''
set @strsql = 'select top ' + str(@pagesize) + ' * from ['
+ @tblname + '] where [' + @fldname + ']' + @strtmp + '(['
+ @fldname + ']) from (select top ' + str((@pageindex-1)*@pagesize) + ' ['
+ @fldname + '] from [' + @tblname + '] where ' + @strwhere + ' '
+ @strorder + ') as tbltmp) and ' + @strwhere + ' ' + @strorder
else
set @strsql = 'select top ' + str(@pagesize) + ' * from ['
+ @tblname + '] where [' + @fldname + ']' + @strtmp + '(['
+ @fldname + ']) from (select top ' + str((@pageindex-1)*@pagesize) + ' ['
+ @fldname + '] from [' + @tblname + ']' + @strorder + ') as tbltmp)'
+ @strorder
endexec (@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...