一个高效的数据分页的存储过程

2016-01-29 18:49 28 1 收藏

一个高效的数据分页的存储过程,一个高效的数据分页的存储过程

【 tulaoshi.com - ASP 】

 

CREATE PROCEDURE pageTest  --用于翻页的测试
--需要把排序字段放在第一列

 (
  @FirstID nvarchar(20)=null,  --当前页面里的第一条记录的排序字段的值
  @LastID nvarchar(20)=null,  --当前页面里的最后一条记录的排序字段的值
  @isNext bit=null,    --true 1 :;false 0:上一页
  @allCount int output,   --返回总记录数
  @pageSize int output,   --返回一页的记录数
  @CurPage int     --页号(第几页)0:第一页;-1最后一页。
  )

AS

if @CurPage=0
 begin
  --统计总记录数
  select @allCount=count(ProductId) from Product_test
  
  set @pageSize=10
  --返回第一页的数据
  select top 10
   ProductId,
   ProductName,
   Introduction  
   from Product_test order by ProductId
 end

else if @CurPage=-1

 select * from
  (select top 10 ProductId,
   ProductName,
   Introduction

  from Product_test order by ProductId desc ) as aa 
  order by ProductId
else

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/asp/)

 begin
  if @isNext=1
   --翻到
   select top 10 ProductId,
   ProductName,
   Introduction
  from Product_test where ProductId @LastID order by ProductId
  
  
  else
   --翻到上一页
   select * from
    (select top 10 ProductId,
   ProductName,
   Introduction
  from Product_test where ProductId < @FirstID  order by ProductId desc) as bb order by ProductId
 end
 

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/asp/)

百万数据翻页就像100条数据一样!

 

来源:https://www.tulaoshi.com/n/20160129/1507397.html

延伸阅读
代码如下: /*  数据库分页存储过程,支持倒序和升序  参数说明:    @tablename:为搜索表名    @tablefield:为表的字段,约定为表的主键,    @where:为搜索表名,要显示所有记录请设为"1=1"    @orderby:为搜索结果排序,如order by id des...
标签: ASP
  if exists(select * from sysobjects where ID = object_id("up_TopicList"))    drop proc up_TopicList go create proc up_TopicList             @a_ForumID int , @a_intDays int , @a_intPageNo int , @a_intPageSize tinyint  &nbs...
其实在很多时候设计的度还是要把握的,不至于让自己陷入的怪圈中才是最重要的,因为我们还要留出时间还解决其他的很多问题,个人认为适度就可以了,留出一定的空间。也因为万能是不存在的,万物在一定的范畴之内都是合理的,出了范畴可能就没有合理的了。          分页存储过程大致有下列几种 1、&n...
代码如下: /* *@curentpage 当前页 *@pagesize 每页记录数 *@TableName 表名 *@key 主键(自动排序) *@where 查询条件 1)空为 null 2)有查询条件不要带where *@order '0'表示 desc '1'是asc *@pageCount 总页数 */ create procedure Page @currentpage int,@pagesize int, @TableName varchar(30),@key varchar(30), @where...
标签: Web开发
CREATE PROC myx_prPageRecordset @queryStr nvarchar(1000),  @keyField nvarchar (200),  @pageSize int,  @pageNumber int  AS BEGIN DECLARE @sqlText AS nvarchar(4000) DECLARE @sqlTable AS nvarchar(4000) SET @sqlTable =&n...

经验教程

842

收藏

54
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部