SQL 分頁儲存過程

2021-10-09 18:05:45 字數 2502 閱讀 1767

go/****** object:  storedprocedure [dbo].[up_getrecordbypage]    script date: 2020-09-24 13:20:25 ******/

drop procedure [dbo].[up_getrecordbypage]

go/****** object:  storedprocedure [dbo].[up_getrecordbypage]    script date: 2020-09-24 13:20:25 ******/

set ansi_nulls on

goset quoted_identifier on

go/****** object:  storedprocedure [dbo].[up_getrecordbypage]    script date: 04/14/2015 16:57:36 ******/

--if  exists (select * from sys.objects where object_id = object_id(n'[dbo].[up_getrecordbypage]') and type in (n'p', n'pc'))

--drop procedure [dbo].[up_getrecordbypage]

--go

create procedure [dbo].[up_getrecordbypage]

(@tblname   varchar(255),       -- 表名

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

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

@pagesize   int = 10,          -- 頁尺寸

@pageindex  int = 1,           -- 頁碼

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

@ordertype  int = 0,  -- 設定排序型別, 0公升序、1降序、2任意欄位的公升和降,需要注意傳入值否則查詢出錯

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

declare @strsql   varchar(8000)       -- 主語句

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

if @isrecount != 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 @strorder ='select row_number() over (order by '+@fldname +' asc) as rownumber, '+@strgetfields+' from ' 

endelse if @ordertype = 1

begin

set @strorder ='select row_number() over (order by '+@fldname +' desc) as rownumber, '+@strgetfields+' from ' 

end         

else if @ordertype = 2

begin

set @strorder ='select row_number() over (order by '+@fldname +') as rownumber, '+@strgetfields+' from ' 

endif @strwhere != ''  

set @strsql = ' select top ' + str(@pagesize) +' * from ( '+@strorder+' '+@tblname+' where '+@strwhere+' ) t '

+'where rownumber >= 1+'+str(@pagesize*(@pageindex-1))

else

set @strsql = ' select top ' + str(@pagesize) +' * from ( '+@strorder+' '+@tblname+' ) t '

+'where rownumber >= 1+'+str(@pagesize*(@pageindex-1))

end  

print(@strsql)

exec (@strsql)

go

sql 儲存過程分頁

create proc myx prpagerecordset querystr nvarchar 1000 keyfield nvarchar 200 pagesize int,pagenumber int as begin declare sqltext as nvarchar 4000 dec...

SQL 儲存過程 分頁

1.俄羅斯儲存過程 的改良版 create procedure pagination1 pagesize int,頁面大小,如每頁儲存20條記錄 pageindex int 當前頁碼 as set nocount on begin declare indextable table id int id...

SQL 分頁儲存過程

create procedure splitpage sql nvarchar 4000 不帶排序語句的sql語句 page int,頁碼 recsperpage int,每頁容納的記錄數 id varchar 255 需要排序的不重複的id號 sort varchar 255 排序欄位及規則 as...