一个分页存储过程代码

复制代码 代码如下:

————————————

–用途:分页存储过程(对有主键的表效率极高)

–说明:

————————————

alter procedure [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)

as

declare @strsql varchar(6000) — 主语句

declare @strtmp varchar(100) — 临时变量

declare @strorder varchar(400) — 排序类型

if @ordertype != 0

begin

set @strtmp = ‘<(select min’

set @strorder = ‘ order by [‘ + @fldname +’] desc’

end

else

begin

set @strtmp = ‘>(select max’

set @strorder = ‘ order by [‘ + @fldname +’] asc’

end

set @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

end

if @isrecount != 0

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

exec (@strsql)

(0)
上一篇 2022年3月21日
下一篇 2022年3月21日

相关推荐