ASP分页存储过程

对于SQLServer数据库,可以在查询分析器中执行以下代码,生成分页存储过程
 cr�ate proc AbsolutePages
(
@PageSize int=0 ,             --每页显示的记录数
    @PageCurrent int= ,           --当前要显示的页号
    @FdName varchar(00)='' ,      --主键名或者标识列名
    @Sel�ctStr varchar(2000)='',   --sel�ct子句,不包含sel�ct关键字,如:*或者id,UserId,UserName等。
    @FromStr varchar(000)='',     --from子句,不包含from关键子,如:myTable或者myTable,yourTable
    @Wh�reStr varchar(2000)='',    --wh�re子句,不包含wh�re关键字,如空的,或者 id>2 等
    @OrderByStr varchar(000)=''  --order by 子句,不包含order by 子句 ,如id desc,UserId asc 等
)
as
------------------定义局部变量------------------
declare @Id varchar(20),@Id2 varchar(20) --开始和结束的记录号
declare @OrderBySqls varchar(000)        --order by 子句
declare @Wh�reSqls varchar(2000)          --wh�re 子句
declare @Sqls nvarchar(4000)              --最终组合成的Sqls语句
declare @TmpStr varchar(2000)             --临时语句
------------
if @Wh&#0;reStr <> ''
set @Wh&#0;reSqls = ' wh&#0;re ('+@Wh&#0;reStr ')'
else
set @Wh&#0;reSqls = ''
------------
if @OrderByStr <> ''
set @OrderBySqls = ' order by ' @OrderByStr
else
set @OrderBySqls = ''
------------
set @TmpStr = @Wh&#0;reSqls
------------------如果显示第一页,可以直接用top来完成------------------
if @PageCurrent <=
begin
sel&#0;ct @Id = convert(varchar(20),@PageSize)
set @Sqls = 'sel&#0;ct top '   @Id   ' '   @Sel&#0;ctStr   ' from '   @FromStr   @Wh&#0;reSqls   @OrderBySqls
exec (@Sqls)
end
else
begin
sel&#0;ct @Id=convert(varchar(20),@PageSize),@Id2=convert(varchar(20),(@PageCurrent-)*@PageSize)
if @Wh&#0;reSqls <> ''
set @Wh&#0;reSqls = @Wh&#0;reSqls   ' and ('   @FdName ' not in(sel&#0;ct top ' @Id2 ' ' @FdName ' from ' @FromStr @Wh&#0;reSqls @OrderBySqls '))'
else
set @Wh&#0;reSqls = ' wh&#0;re '   @FdName ' not in(sel&#0;ct top ' @Id2 ' ' @FdName ' from ' @FromStr @Wh&#0;reSqls @OrderBySqls ')'
----------
 set @Sqls = 'sel&#0;ct top ' @Id  ' '  @Sel&#0;ctStr ' from ' @FromStr @Wh&#0;reSqls @OrderBySqls
exec (@Sqls)
end
print @Sqls
return
GO
高级分页存储过程
 cr&#0;ate proc AbsolutePageEx
(
@PageSize int=0,             --每页要显示的记录条数
@PageCurrent int=,           --要显示第几页
@table nvarchar(4000),        --表名、视图名、记录集、查询结果
@Fields nvarchar (4000)='',   --要显示的字段列表(如果查询结果有标识字段,必须指定此值,且不包含标识字段)
@order nvarchar (000)=''     --排序字段列表
)
as
begin
declare @FdName nvarchar(250) --表中的主键或表、临时表中的标识列名
 declare @Id varchar(20),@Id2 varchar(20) --开始和结束的记录号
 declare @Obj_ID int --对象id
 --表中有复合主键的处理
 declare @strfd nvarchar(2000) --复合主键列表
 declare @strjoin nvarchar(4000) --连接字段
 declare @strwh&#0;re nvarchar(2000) --查询条件
 --格式化表、字段、排序字段的参数形式
 sel&#0;ct @Obj_ID=object_id(@table)
,@Fields=case isnull(@Fields,'') when '' then ' *' else ' '+@Fields end
,@order=case isnull(@order,'') when '' then '' else ' order by ' @order end
,@table=case when @Obj_ID is not null then ' ' @table else ' (' @table ') a' end
------如果要显示第一页,可以直接用top来完成------
 if @PageCurrent=
begin
sel&#0;ct @Id=cast(@PageSize as varchar(20))
exec('sel&#0;ct top ' @Id @Fields ' from ' @table @order)
return
end
------如果是表,则检查表中是否有标识更或主键------
 if @Obj_ID is not null and objectproperty(@Obj_ID,'IsTable')=
begin
set @Id=cast(@PageSize as varchar(20))
set @Id2=cast((@PageCurrent-)*@PageSize as varchar(20))
sel&#0;ct @FdName=name from syscolumns wh&#0;re id=@Obj_ID and status=0x80
if @@rowcount=0 --如果表中无标识列,则检查表中是否有主键
  begin
if not exists(sel&#0;ct  from sysobjects wh&#0;re parent_obj=@Obj_ID and xtype='PK')
goto lbusetemp --如果表中无主键,则用临时表处理

sel&#0;ct @FdName=name from syscolumns wh&#0;re id=@Obj_ID and colid in(
sel&#0;ct colid from sysindexkeys wh&#0;re @Obj_ID=id and indid in(
sel&#0;ct indid from sysindexes wh&#0;re @Obj_ID=id and name in(
sel&#0;ct name from sysobjects wh&#0;re xtype='PK' and parent_obj=@Obj_ID
)))
if @@rowcount> --检查表中的主键是否为复合主键
   begin
sel&#0;ct @strfd='',@strjoin='',@strwh&#0;re=''
sel&#0;ct @strfd=@strfd ',[' name ']'
,@strjoin=@strjoin ' and a.[' name ']=b.[' name ']'
,@strwh&#0;re=@strwh&#0;re ' and b.[' name '] is null'
from syscolumns wh&#0;re id=@Obj_ID and colid in(
sel&#0;ct colid from sysindexkeys wh&#0;re @Obj_ID=id and indid in(
sel&#0;ct indid from sysindexes wh&#0;re @Obj_ID=id and name in(
sel&#0;ct name from sysobjects wh&#0;re xtype='PK' and parent_obj=@Obj_ID
)))
sel&#0;ct @strfd=substring(@strfd,2,2000)
,@strjoin=substring(@strjoin,5,4000)
,@strwh&#0;re=substring(@strwh&#0;re,5,4000)
goto lbusepk
end
end
end
else
goto lbusetemp
/*--使用标识列或主键为单一字段的处理方法--*/
 lbuseidentity:
exec('sel&#0;ct top ' @Id @Fields ' from ' @table
' wh&#0;re ' @FdName ' not in(sel&#0;ct top '
@Id2 ' ' @FdName ' from ' @table @order
')' @order
)
return
/*--表中有复合主键的处理方法--*/
 lbusepk:
exec('sel&#0;ct ' @Fields ' from(sel&#0;ct top ' @Id ' a.* from
(sel&#0;ct top 00 percent * from ' @table @order ') a
left join (sel&#0;ct top ' @Id2 ' ' @strfd '
from ' @table @order ') b on ' @strjoin '
wh&#0;re ' @strwh&#0;re ') a'
)
return
/*--用临时表处理的方法--*/
 lbusetemp:
sel&#0;ct @FdName='[ID_' cast(newid() as varchar(40)) ']'
,@Id=cast(@PageSize*(@PageCurrent-) as varchar(20))
,@Id2=cast(@PageSize*@PageCurrent- as varchar(20))
exec('sel&#0;ct ' @FdName '=identity(int,0,),' @Fields '
into #tb from ' @table @order '
sel&#0;ct ' @Fields ' from #tb wh&#0;re ' @FdName ' between '
@Id ' and ' @Id2
)
end
GO


[本日志由 小戒 于 2008-05-27 08:42 AM 编辑]
上一篇: 关于ASP的简单分页
下一篇: 提高ASP的SQL执行效率
文章来自:
引用通告: 查看所有引用 | 我要引用此文章
Tags: ASP 分页 存储过程 过程
相关日志:
评论: 0 | 引用: 49 | 查看次数: -
发表评论
昵 称:
密 码: 游客发言不需要密码.
邮 箱: 支持Gravatar头像.
网 址: 输入网址便于回访.
内 容:
验证码:
选 项:
虽然发表评论不用注册,但是为了保护您的发言权,建议您注册帐号.