sql 2000 分页存储过程


EXEC AspNetPage 'Bao_Supplydown','id',10,2,0,'id,fclass,infname,content,addtime,isshow,downcount','addtime DESC',''

/****** 对象: 存储过程 dbo.AspNetPage  ******/
/*
=====================================
 * @Describe 通用查询数据ASPNETPAGE分页过程
/*   
  @Tables       表名                      必选    
  @PrimaryKey   主关键字                  必选   
  @pagesize     页码大小                  可选    默认值:1   
  @pageindex    当前页                    可选    默认值:1   
  @docount      是否只统计总记录数        可选   默认值:否   
  @Fields       选择字段                  可选   默认:所有字段   
  @Sort         排序语句,不带Order   By    可选   
  @Filter       过滤语句,不带Where       可选   
  
*/   
=====================================
*/
CREATE     procedure   AspNetPage
  (   
  
/*   
  @Tables           表名                                         必选     
  @PrimaryKey   主关键字                                 必选   
  @pagesize       页码大小                                 可选     默认值:1   
  @pageindex     当前页                                     可选     默认值:1   
  @docount         是否只统计总记录数             可选   默认值:否   
  @Fields           选择字段                                 可选   默认:所有字段   
  @Sort               排序语句,不带Order   By     可选   
  @Filter           过滤语句,不带Where           可选   
  
*/   
  
@Tables   nvarchar(400),   
  
@PrimaryKey   nvarchar(100),   
  
@pagesize   int=1,   
  
@pageindex   int=1,   
  
@docount   bit=0,   
  
@Fields   varchar(1000)   =   '*',   
  
@Sort   varchar(1000)   =   NULL,   
  
@Filter   varchar(1000)   =   NULL)   
  
as   
  
set   nocount   on   
    
  
DECLARE   @strFilter   varchar(1000)   
  
DECLARE   @strSort   varchar(1000)   
  
IF   @Filter   IS   NOT   NULL   AND   @Filter   !=   ''   
  
BEGIN   
  
SET   @strFilter   =   '   WHERE   '   +   @Filter   +   '   '   
  
END   
  
ELSE   
  
BEGIN   
  
SET   @strFilter   =   ''   
  
END   
  
IF   @Sort   IS   Not   NULL   And   @Sort!=''   
                  
Begin   
                                  
Set   @strSort='order   by   '+@Sort+''   
                  
End   
                            
  
if(@docount=1)   
    
  
exec('select   count('+@PrimaryKey+')   from   '+@Tables+'   '+@strFilter+'')   
  
else   
  
begin   
  
declare   @PageLowerBound   int   
  
declare   @PageUpperBound   int   
  
set   @PageLowerBound=(@pageindex-1)*@pagesize   
  
set   @PageUpperBound=@PageLowerBound+@pagesize   
  
create   table   #pageindex(id   int   identity(1,1)   not   null,nid   int)   
  
set   rowcount   @PageUpperBound   
  
exec(   
  
'   
  insert   into   #pageindex(nid)   
  select   
'+@PrimaryKey+'   from   '+@Tables+'   '+@strFilter+'   '+@strSort+'   
  select   O.
'+@Fields+'   
  from   
'+@Tables+'   O,#pageindex   p   
  where   O.
'+@PrimaryKey+'=p.nid   and   p.id>'+@PageLowerBound+'   and   p.id<='+@PageUpperBound+'   order   by   p.id   
  
'   
  )   
select * from #pageindex
  
end   
  
set   nocount   off

GO
原文地址:https://www.cnblogs.com/pipizhu/p/1631248.html