轻松应付百万数据的数据分页存储过程

2016-02-19 13:50 5 1 收藏

岁数大了,QQ也不闪了,微信也不响了,电话也不来了,但是图老师依旧坚持为大家推荐最精彩的内容,下面为大家精心准备的轻松应付百万数据的数据分页存储过程,希望大家看完后能赶快学习起来。

【 tulaoshi.com - 编程语言 】

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

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

(
@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

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

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

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

来源:https://www.tulaoshi.com/n/20160219/1605379.html

延伸阅读
标签: SQLServer
/*--用存储过程实现的分页程序 显示指定表、视图、查询结果的第X页 对于表中主键或标识列的情况,直接从原表取数查询,其它情况使用临时表的方法 如果视图或查询结果中有主键,不推荐此方法 --邹建 2003.09--*/ /*--调用示例 exec p_show '地区资料' exec p_show '地区资料',5,3,'地区编号,地区名称,助记码','地区编号' --*/ /* 因为要顾及...
标签: ASP
  建立表: CREATE TABLE [TestTable] ( [ID] [int] IDENTITY (1, 1) NOT NULL , [FirstName] [nvarchar] (100) COLLATE Chinese_PRC_CI_AS NULL , [LastName] [nvarchar] (100) COLLATE Chinese_PRC_CI_AS NULL , [Country] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NULL , [Note] [nvarchar] (2000) COLLATE Chinese_PRC_CI_...
怎样才能将在表A取得的数据插入另一个表B中? (1)对于表A和表B两个表结构完全相同的话〔字段个数,相应字段的类型等等〕,可以使用 insert INTO B select * FROM A; insert INTO B(field1,field2,field3) select A.field1,A.field2,A.field3 from A; (2) 对于两个表如果字段数不一样,但是有几个字段的结构一样时...
标签: ASP
  /*数据结构*/ /*bbs用户表*/ if exists(select * from sysobjects where id = object_id('BBSUser')) drop table BBSUser go create table BBSUser ( id int identity primary key , UserName varchar(20) default '' not null , Password varchar(10) default '' not null , Email varchar(100) default '' not null , Homepage var...
标签: ASP
  /*************************************************************************/ /* */ /* procedure : up_GetForumList */ /* */ /* Description: 取得版面列表 */ /* */ /* Parameters: None */ /* */ /* Use table: forum , bbsuser */ /* */ /* Author: bigeagle@163.net */ /* */ /* Date: 2000/2/10 */ /* */ /* History: */ /* *...

经验教程

380

收藏

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