最新分頁儲存過程

2021-08-29 21:17:44 字數 1678 閱讀 8162

/*

資料庫分頁儲存過程,支援倒序和公升序

引數說明:

@tablename:為搜尋表名

@tablefield:為表的字段,約定為表的主鍵,

@where:為搜尋表名,要顯示所有記錄請設為"1=1"

@orderby:為搜尋結果排序,如order by id desc

@fieldlist:為字段列表,如userid, username

@curpage:當前頁碼

@page_record:每頁記錄條數

@sort:排序標識(如果是倒序排,引數值為desc,為公升序,引數值為asc,跟orderby引數是對應的)

結果: 返回表tablename中滿足條件where的第curpage頁的page_record條記錄,結果按orderby排序

*/create procedure proc_commonpaging

@tablename varchar(100),

@tablefield varchar(20),

@where varchar(5000),

@orderby varchar(500),

@fieldlist varchar(1000),

@curpage int,

@page_record int,

@sort varchar(8)

asbegin

declare @cmd varchar(8000)

declare @uprecord int

declare @op varchar(2) -- 操作符

declare @max_min varchar(4) -- 最大/最小計算

set @op = '<'

set @max_min = 'min'

if @sort = 'asc'

begin

set @op = '>'

set@max_min = 'max'

end

set @uprecord=@curpage * @page_record

if @curpage = 0

set @cmd = 'select top '+cast(@page_record as nvarchar)+' '+@fieldlist+' from '+@tablename+' where '+@where+' '+@orderby

else

set @cmd = 'select top '+cast(@page_record as nvarchar)+' '+@fieldlist+' from '+@tablename+' where '+@where+' and '+@tablefield+'

'+@op+' (select '+@max_min+'('+@tablefield+')   from (select top   '+cast(@uprecord as nvarchar)+' '+@tablefield+' from '+@tablename+' where

'+@where+' '+@orderby+') as tmptbl ) and '+@where+' '+@orderby

set @cmd = @cmd + '; select count(*) from '+@tablename+' where '+@where

exec(@cmd)

print(@cmd)

endgo

分頁儲存過程 分頁儲存過程

分頁儲存過程 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...