分頁儲存過程

2021-09-06 01:22:50 字數 4336 閱讀 1619

1,

------------------------------------

--用途:支援任意排序的分頁儲存過程

--說明:

------------------------------------

create procedure [dbo].[up_getrecordbypageorder]

@tblname varchar(255),   -- 表名

@fldname varchar(255),   -- 顯示欄位名

@orderfldname varchar(255), -- 排序欄位名

@statfldname varchar(255), -- 統計欄位名

@pagesize int = 10,   -- 頁尺寸

@pageindex int = 1,   -- 頁碼

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

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

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

asdeclare @strsql varchar(6000) -- 主語句

declare @strtmp varchar(100)   -- 臨時變數(查詢條件過長時可能會出錯,可修改100為1000)

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

if @ordertype != 0

begin

set @strtmp = '<(select min'

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

endelse

begin

set @strtmp = '>(select max'

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

endset @strsql = 'select top ' + str(@pagesize) + ' ' + @fldname + ' from ['

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

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

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

+ @strorder

if @strwhere != ''

set @strsql = 'select top ' + str(@pagesize) + ' ' + @fldname + ' from ['

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

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

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

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

if @pageindex = 1

begin

set @strtmp = ''

if @strwhere != ''

set @strtmp = ' where ' + @strwhere

set @strsql = 'select top ' + str(@pagesize) + ' ' + @fldname + ' from ['

+ @tblname + ']' + @strtmp + ' ' + @strorder

endif @isrecount != 0

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

if @strwhere!=''

set @strsql = @strsql+' where ' + @strwhere

exec (@strsql)

2,------------------------------------

--用途:分頁儲存過程(對有主鍵的表效率極高)

--說明:

------------------------------------

create procedure [dbo].[up_getrecordbypage]

@tblname      varchar(255),       -- 表名

@fldname      varchar(255),       -- 主鍵欄位名

@pagesize     int = 10,           -- 頁尺寸

@pageindex    int = 1,            -- 頁碼

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

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

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

asdeclare @strsql   varchar(6000)       -- 主語句

declare @strtmp   varchar(100)        -- 臨時變數(查詢條件過長時可能會出錯,可修改100為1000)

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

if @ordertype != 0

begin

set @strtmp = '<(select min'

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

endelse

begin

set @strtmp = '>(select max'

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

endset @strsql = 'select top ' + str(@pagesize) + ' * 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) + ' * from ['

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

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

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

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

if @pageindex = 1

begin

set @strtmp =''

if @strwhere != ''

set @strtmp = ' where ' + @strwhere

set @strsql = 'select top ' + str(@pagesize) + ' * from ['

+ @tblname + ']' + @strtmp + ' ' + @strorder

endif @isrecount != 0

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

exec (@strsql)

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

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