j0057 4 +357

just a test-黑暗中的矩阵,寒星光芒四射!

导航

分页存储过程

网上有很多关于此存储过程的转载可是没有一个写有清晰的注释,为此研究后增加了注释。

CREATE procedure main_table_pwqzc
-----------------------------------------------------------------
--定义变量
-----------------------------------------------------------------
(@pagesize int,--页面大小
@pageindex int,--页的序号
@docount bit,--是否浏览全部记录
@this_id)/*对此存有疑问?????没有用,定义变量却没有指定数据类型*/
as
-----------------------------------------------------------------
if(@docount=1)
begin
select count(id) from luntan where
this_id=@this_id
end
else
begin
declare @indextable table(id int identity(1,1),nid int)/*定义临时表*/
declare @PageLowerBound int
declare @PageUpperBound int
set @PageLowerBound=(@pageindex-1)*@pagesize/*最小值*/
set @PageUpperBound=@PageLowerBound+@pagesize/*最大值*/
set rowcount @PageUpperBound /*设置返回受影响的行数为最大值*/
/*选择符合条件的ID插入到表@indextable*/
insert into @indextable(nid) select id from luntan where
this_id=@this_id order by reply_time desc
/*从实际的表中选出ID相等的记录*/
select a.* from luntan a,@indextable t where a.id=t.nid
and t.id>@PageLowerBound and t.id<
=@PageUpperBound order by t.id
end
GO
----------------------------------------------------------------------------
/*存储过程会根据传入的参数@docount来确定是不是要返回所有要分页的记录总数
特别是这两行

××××××××××××××××××××××××××××××
set rowcount @PageUpperBound
insert into @indextable(nid) select id from luntan where
this_id=@this_id order by reply_time desc

××××××××××××××××××××××××××××××
真的是妙不可言!!set rowcount @PageUpperBound当记录数达到@PageUpperBound时就会停止处理查询
,select id 只把id列取出放到临时表里,select a.* from luntan a,@indextable t where a.id=t.nid
and t.id>@PageLowerBound and t.id<
=@PageUpperBound order by t.id 而这句也只从表中取出所需要的记录,而不是所有的记录,结合起来,极大的提高了效率!!
妙啊,真的妙!*/

http://www.codeproject.com/KB/aspnet/PagingLarge.aspx:这位老外大哥总结的。

posted on 2008-09-16 00:05  君宝  阅读(397)  评论(1编辑  收藏  举报