高效能分頁儲存過程

2021-06-08 20:04:49 字數 1929 閱讀 7302

select count(1) from dbo.gatherkeywordinfo

dbo.p_partpage 'gatherkeywordinfo','gatherkeywordid',5,1000,'','',''

exec [dbo].[p_partpage] 'v_gatherkeywordinfo','gatherkeywordid','*',5,1000,'','newid()',''

select top 10 * from v_gatherkeywordinfo

if(object_id('p_partpage') is not null)

drop proc [dbo].[p_partpage]

gocreate procedure [dbo].[p_partpage]

@tablename varchar(50),

@tableid varchar(50),

@pageindex int,         -- 當前頁索引

@pagesize int,            -- 每頁顯示多少條

@strwhere varchar(500), -- 查詢條件

@order varchar(50),  -- 排序 如: id asc,time desc

@fields varchar(1000) = '*'--讀取字段

asdeclare @strfilter varchar(2000)

declare @sql varchar(8000)

if @strwhere is not null and @strwhere != ''

begin

set @strfilter = ' where ' + @strwhere + ' '

endelse

begin

set @strfilter = ''

endif @ordercol = ''

set @ordercol = @tableid

if @pageindex < 1

set @pageindex = 1

if @pageindex = 1 --第一頁提高效能

begin

set @sql = 'select top ' + str(@pagesize) +' '+@fields+ '  from ' + @tablename + ' ' + @strfilter + ' order by  '+ @order

end

else

begin

/**//*execute dynamic query*/    

declare @start_id varchar(50)

declare @end_id varchar(50)

set @start_id = convert(varchar(50),(@pageindex - 1) * @pagesize + 1)

set @end_id = convert(varchar(50),@pageindex * @pagesize)

set @sql =  ' select '+@fields+ '

from (select row_number() over(order by '+@ordercol+') as rownum,

'+@fields+ '

from '+@tablename+' ' +@strfilter+') as d

where rownum between '+@start_id+' and ' +@end_id +' order by '+@order

endset @sql = @sql + '; select  count(*) from ' + @tablename + @strfilter  

exec(@sql)

goselect * from sysobjects where name = 'p_partpage'

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

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