首页 > 开发 > 综合 > 正文

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

2024-07-21 02:05:52
字体:
来源:转载
供稿:网友

最大的网站源码资源下载站,


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 <=0 or @curpage <0
    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
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表