分頁儲存過程

2021-06-21 05:13:39 字數 4202 閱讀 1403

create procedure page

@tblname varchar(255), -- 表名

@strgetfields varchar(1000)='*', -- 需要返回的列

@fldname varchar(255)='', -- 排序的欄位名

@pagesize int=10, -- 頁尺寸

@pageindex int=1, -- 頁碼

@docount bit=0, -- 返回記錄總數, 非 0 值則返回

@ordertype bit=0, -- 設定排序型別, 非 0 值則降序

@strwhere varchar(1500)='' -- 查詢條件 (注意: 不要加 where)

as declare @strsql varchar(5000) -- 主語句

declare @strtmp varchar(110) -- 臨時變數

declare @strorder varchar(400) -- 排序型別

if @docount!=0

begin

if @strwhere!=''

set @strsql = 'select count(*) as total from ['+@tblname+'] where '+

@strwhere

else

set @strsql = 'select count(*) as total from ['+@tblname+']'

end--以上**的意思是如果@docount傳遞過來的不是0,就執行總數統計。以下的所有**都是@docount為0的情況

else

begin

if @ordertype!=0

begin

set @strtmp = '<(select min'

set @strorder = ' order by ['+@fldname+'] desc'

--如果@ordertype不是0,就執行降序,這句很重要!

endelse

begin

set @strtmp = '>(select max'

set @strorder = ' order by ['+@fldname+'] asc'

end

if @pageindex=1

begin

if @strwhere!=''

set @strsql = 'select top '+str(@pagesize)+' '+@strgetfields+

' from ['+@tblname+'] where '+@strwhere+' '+@strorder

else

set @strsql = 'select top '+str(@pagesize)+' '+@strgetfields+

' from ['+@tblname+'] '+@strorder

--如果是第一頁就執行以上**,這樣會加快執行速度

endelse

begin

--以下**賦予了@strsql以真正執行的sql**

set @strsql = 'select top '+str(@pagesize)+' '+@strgetfields+

' from ['

+@tblname+'] where ['+@fldname+']'+@strtmp+'(['+@fldname+

']) from (select top '+str((@pageindex-1)*@pagesize)+' ['+@fldname

+'] from ['+@tblname+']'+@strorder+') as tbltmp)'+@strorder

if @strwhere!=''

set @strsql = 'select top '+str(@pagesize)+' '+@strgetfields+

' from ['

+@tblname+'] where ['+@fldname+']'+@strtmp+'(['

+@fldname+']) from (select top '+str((@pageindex-1)*@pagesize)

+' ['

+@fldname+'] from ['+@tblname+'] where '+@strwhere+' '

+@strorder+') as tbltmp) and '+@strwhere+' '+@strorder

endend

exec (@strsql)

go

--呼叫:

-- page '表','*','id',123456,9,0

--游標查詢

create procedure cursorpage

@sqlstr nvarchar(4000), --查詢字串

@currentpage int, --第n頁

@pagesize int --每頁行數

as

set nocount on

declare @p1 int, --p1是游標的id

@rowcount int

exec sp_cursoropen @p1 output,@sqlstr,@scrollopt=1,@ccopt=1,@rowcount=@rowcount output

select ceiling(1.0*@rowcount/@pagesize) as 總頁數,@rowcount as 總行數,@currentpage as 當前頁

set @currentpage=(@currentpage-1)*@pagesize+1

exec sp_cursorfetch @p1,16,@currentpage,@pagesize

exec sp_cursorclose @p1

set nocount off

go--呼叫

exec cursorpage 'select * from dbo.費用表 where 銷帳日期=''2010-05-16 00:00:00.000''',1,10

create proc page @pagelinenum int,--一頁顯示多少行

@pagenum int --第幾頁

as select *

from 費用表

where 費用id between @pagelinenum * ( @pagenum - 1 ) + 1 and @pagelinenum * @pagenum

go--呼叫

page 30, 56

--2012

declare @pagesize tinyint = 5,

@currentpage int = 1;

select businessid,cityid,serviceshopname

from business_login

where isshow=0

order by orderid

offset (@pagesize * (@currentpage - 1))

rows

fetch next @pagesize rows only;

--05

declare @pagesize as int, @pagenum as int;

set @pagesize = 5;

set @pagenum = 1;

with salesrn as

( select row_number() over(order by orderid) as rownum,

businessid,cityid,serviceshopname

from business_login

where isshow=0

)select rownum,businessid,cityid,serviceshopname

from salesrn

where rownum > @pagesize * (@pagenum-1)

and rownum <= @pagesize * @pagenum

order by rownum;

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

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