分頁儲存過程

2021-04-20 01:38:03 字數 2062 閱讀 9644

create procedure [dbo].[getdatabypage]

(@sql nvarchar(4000),

@currentpage int,

@recsperpage nvarchar(50),

@selectfield varchar(255),

@sortfield varchar(255))as

declare @str nvarchar(4000)

if(@currentpage=1)

begin

set @str = 'select top '+@recsperpage + substring(@sql,7,3000)+' order by '+@sortfield+' desc'

endelse

begin

set @str='select   top '+cast(@recsperpage as varchar(20))+' * from ('+@sql+') t where t.'+@selectfield+' not in

(select   top '+cast((@recsperpage*(@currentpage-1)) as varchar(20))+' '+@selectfield+' from ('+@sql+') t9 order by '+@sortfield+' desc) order by '+@sortfield+' desc'

endexec sp_executesql @str

***** 2 ****************************************=

create procedure  getcompanydatapage

@pageindex int,

@pagesize  int,

@order varchar(24),

@method varchar(8),

@where varchar(200),

@recordcount int,

@pagecount int out

asdeclare @methodd varchar(8)

if @method = 'asc'

set @methodd = 'desc'

else if @method = 'desc'

set @methodd = 'asc'

--select @recordcount=count(*) from company where  +@where

set @pagecount = ceiling(@recordcount * 1.0 / @pagesize)

declare @sqlstr nvarchar(300)

if @pageindex = 0 or @pagecount <= 1

set @sqlstr =n'select top '+str( @pagesize )+

' *  from company where '+@where+' order by '+@order+' '+@method

else if  @pageindex = @pagecount - 1

set @sqlstr =n'select * from (select top '+str( @recordcount - @pagesize * @pageindex )

+' *  from   company where '+@where+' order by '+@order+' '+@methodd

+') temptable order by '+@order+' '+@method

else

set @sqlstr =n'select top  '+str( @pagesize )+' * from ( select top '

+str( @recordcount - @pagesize * @pageindex )+' *  from  company where '

+@where+' order by '+@order+' '+@methodd+') temptable order by  '+@order+' '+@method

exec (@sqlstr)

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...