您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

一个比较实用的大数据量分页存储过程

2026/1/9 12:30:56发布12次查看
一个比较实用的大数据量分页存储过程
create proc sp_publicturnpagewebsite(
@tbname nvarchar(100)='', --表名,如 pinyin
@pagesize int=10, --每页的记录数,默认为 10
@curpage int=1, --表示当前页 1
@keyfield nvarchar(100)='id', --关键字段名,默认为 id,该字段要求是表中的索引 或 无重复和不为空的字段
@keyascdesc nvarchar(4)='asc', --关键字的升、降序,默认为升序 asc , 降序为 desc
@fields nvarchar(500)='*', --所选择的列名,默认为全选
@condition nvarchar(200)='', --where 条件,默认为空
@order nvarchar(200)='' --排序条件,默认为空
) with encryption as
if @tbname = ''
begin
raiserror('请指定表名!',11,1)
return
end
if @pagesize begin
raiserror('当前页数和每页的记录数都必须大于零!',11,1)
return
end
if @keyascdesc = 'desc'
set @keyascdesc = 'else
set @keyascdesc = '>'
if @condition ''
set @condition = ' where ' + @condition
declare @sql nvarchar(2000)
set @sql = ''
if @curpage = 1
set @sql = @sql + 'select top ' + cast(@pagesize as nvarchar(20)) + ' ' + @fields + ' from ' + @tbname + @condition + ' ' + @order
else
begin
declare @itopnum int
set @itopnum = @pagesize * (@curpage - 1)
set @sql = @sql + 'declare @slastvalue nvarchar(100)' + char(13)
set @sql = @sql + 'select top ' + cast(@itopnum as nvarchar(20)) + ' @slastvalue=' + @keyfield + ' from ' + @tbname + @condition + ' ' + @order + char(13)
declare @condition2 nvarchar(200)
if @condition = ''
set @condition2 = ' where ' + @keyfield + @keyascdesc + '@slastvalue '
else
set @condition2 = ' and ' + @keyfield + @keyascdesc + '@slastvalue '
set @sql = @sql + 'select top ' + cast(@pagesize as nvarchar(20)) + ' ' + @fields + ' from ' + @tbname + @condition + @condition2 + @order
end
execute sp_executesql @sql
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product