每每面试,总会有公司问到分页。在下不才,在这里写几种分页,望路过的各位大神尽情拍砖。

先从创建数据库说起。源码如下

一.创建数据库

 /**********************************************************************
一.创建数据库DBTest
@author: Alex Tian
Create Date: 2014-03-19
***********************************************************************/
use master --这里我们选择master数据库的目的是为了我们可以访问表
--判断数据库清单中是否存在数据库DBTest
if exists(select * from sysdatabases where name='DBTest')
Drop DataBase DBTest --删除数据库DBTest
Go
/*创建数据库的SQL语句,这里我们就创建DBTest数据库*/
Create Database DBTest
on primary --默认就是primary文件组,可省略
(
/*--数据文件的具体描述--*/
name='DBTest_data', -- 主数据文件的逻辑名称
filename='D:\SQL\DBTest_data.mdf', -- 主数据文件的物理名称
size=5mb, --主数据文件的初始大小
maxsize=100mb, -- 主数据文件增长的最大值
filegrowth=15%--主数据文件的增长率
)
log on
(
/*--日志文件的具体描述,各参数含义同上--*/
name='DBTest_log',
filename='D:\SQL\DBTest_log.ldf',
size=2mb,
filegrowth=1mb
)

二.创建表

 /**********************************************************************
二.创建表Users
***********************************************************************/
use DBTest --选择我们刚刚创建的数据库DBTest
Go
if Exists (select * from sysobjects where name='Users')
Drop Table Users
go
Create Table Users
(
ID int identity(1,1) primary key, --表示是主键自增,标示种子是1.
UName nvarchar(20) Not null, --用户姓名不能为空
USex char(2) --性别
)

三.插入数据

 /**********************************************************************
三.插入数据到表Users
***********************************************************************/
insert into Users
select 'yoyo','男'
union
select 'Alex','男'
union
select '兰阳','女'
union
select '彭伟','男'
union
select '张琼','男'
union
select '肖小仙','女'
union
select '毛毛','男'
union
select '田勇','男'
union
select '田红','男'
union
select '柯丽','女'
union
select 'Gross','男'
union
select '何军','男'
union
select 'Leo','男'
union
select '金琼','女'
union
select '孙龙','男'
union
select '老姚','男'
union
select '李聪','女'
union
select '王超','男'
union
select '孙艳','女'
union
select '曹瑞','男'
union
select '王琼','女'
union
select '沈炎','男'
union
select '庄雪月','女'
union
select '老丁','男'
union
select '景天','男'
union
select '雪见','女'
Go

由于数据量太少,我这里重复插入了上面的测试数据,然后我们查询当前的表Users
 
   上面都是准备工作,废话少说。直接插入主题。

1.下面我们用not in语句去分页,为了方便大家看,直接存储过程附上。

 select top 10 * from Users where (ID not in (select top 20 ID from Users order by ID asc) )
order by ID create procedure sp_Page_View_with_not_in
(
@pageIndex int,--页索引。
@PageSize int--页记录数
)
as
begin
set nocount on
declare @strSQL varchar(1000)
set @strSQL='(select top '+str(@PageSize)+' * from Users where (ID not In (select top '+str(@pageIndex*@PageSize)+' ID from Users order by ID asc)) order by ID)'
set nocount off
end
--drop procedure Page_View_with_not_in --删除存储过程

2.用Max分页,源码如下

 --2.使用select top 和select Max(列键)

 select top 10 * from Users where
ID>
(
select MAX(ID) from
(
select top 20 ID from Users order by ID
) as temp
)
order by ID --创建存储过程
create procedure sp_Page_View_with_Max
(
@PageInde int, -- 当前页索引
@PageSize int --每页要显示的条数
)
as
begin
declare @strSQL nvarchar(1000)
set @strSQL='select top '+str(@PageSize)+' * from Users where ID>(select MAX(ID) from (select top +'+str(@PageInde*@PageSize)+' ID from Users order by ID) as Temp )
order by ID'
end --drop procedure sp_Page_View_with_Max

3.用ROW_NUMBER()分页(仅支持SQL Server 2005或2005之后的数据库),源码如下

 --3.利用Row_number()给数据行加索引(适用于,有很多数据表中没有identity ID的表)
--假设我们当前的Users表没有identity
select RID,UName,USex from
(
select *,ROW_NUMBER() over (order by ID) as RID
from Users
)
as tempTable
where RID>20 and RID<=30 create procedure sp_Page_View_With_ROW_NUMBER
(
@pageIndex int,
@pageSize int
)
as
begin
declare @strSQL nvarchar(1000)
set @strSQL='select RID,UName,USex from (select *,ROW_NUMBER() over (order by ID asc) as RID from Users) as TempTable
where RID>'+str(@pageIndex*@pageSize)+' and RID<='+str(@pageSize*(@pageIndex+1))+''
end

以上是很常用的几种分页,当然还有很多种,比如 用临时表及Row_number ,互联网的大神们用select max方法用2分法等等等。
这里由于数据量很小,我没有过多的去考虑性能。在这里我不对性能方面作于评价。仅用于面试之类的孩纸们了解一下。

SQL Server 存储过程分页的更多相关文章

  1. SQL Server 存储过程 分页查询

    Transact-SQL中的存储过程,非常类似于Java语言中的方法,它可以重复调用.当存储过程执行一次后,可以将语句缓存中,这样下次执行的时候直接使用缓存中的语句.这样就可以提高存储过程的性能. Ø ...

  2. sql server存储过程分页,行变列

    CREATE PROCEDURE [dbo].[PROC_GetPriviousAndNextDetailContent]@Index varchar(20),--表主键@Table varchar( ...

  3. sql server存储过程分页

    Create PROCEDURE [dbo].[Table_GetList] ) = '', -- 查询条件(注意: 不要加 WHERE) ) = '', -- 设置排序 , -- 页尺寸 , -- ...

  4. SQL Server 存储过程分页。

     create proc proc_Product@page int, -- 页数@row int --  一页有几行Asdeclare @newpage int  set @newpage = (@ ...

  5. 浅谈SQL Server数据库分页

    数据库分页是老生常谈的问题了.如果使用ORM框架,再使用LINQ的话,一个Skip和Take就可以搞定.但是有时由于限制,需要使用存储过程来实现.在SQLServer中使用存储过程实现分页的已经有很多 ...

  6. SQL Server 存储过程(转载)

    SQL Server 存储过程 Transact-SQL中的存储过程,非常类似于Java语言中的方法,它可以重复调用.当存储过程执行一次后,可以将语句缓存中,这样下次执行的时候直接使用缓存中的语句.这 ...

  7. (摘录)SQL Server 存储过程

    文章摘录:http://www.cnblogs.com/hoojo/archive/2011/07/19/2110862.html SQL Server 存储过程 Transact-SQL中的存储过程 ...

  8. Sql Server 存储过程中查询数据无法使用 Union(All)

    原文:Sql Server 存储过程中查询数据无法使用 Union(All) 微软Sql Server数据库中,书写存储过程时,关于查询数据,无法使用Union(All)关联多个查询. 1.先看一段正 ...

  9. SQL Server存储过程中使用表值作为输入参数示例

    这篇文章主要介绍了SQL Server存储过程中使用表值作为输入参数示例,使用表值参数,可以不必创建临时表或许多参数,即可向 Transact-SQL 语句或例程(如存储过程或函数)发送多行数据,这样 ...

随机推荐

  1. Unity的Profiler性能分析

    1. CPU Usage A. WaitForTargetFPS: Vsync(垂直同步)功能所,即显示当前帧的CPU等待时间 B. Overhead: Profiler总体时间-所有单项的记录时间总 ...

  2. 如何成为一名优秀的C程序员

    如何成为一名优秀的C程序员 英文原文:To become a good C programmer 问题的提出 每过一段时间我总会收到一些程序员发来的电子邮件,他们会问我是用什么编程语言来编写自己的游戏 ...

  3. Web工程软件升级之数据库升级(二)

    升级前检查 1. 用户是否存在 awk -F: '{if($1 == "xxx") print $1}' /ect/passwd 用户名 awk -F: '{if($1 == &q ...

  4. Spark运行各个时间段的解释

    package org.apache.spark.ui private[spark] object ToolTips {  val SCHEDULER_DELAY =    ""& ...

  5. BZOJ1726: [Usaco2006 Nov]Roadblocks第二短路

    1726: [Usaco2006 Nov]Roadblocks第二短路 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 768  Solved: 369[S ...

  6. 【转】Java删除文件夹和文件

    原文网址:http://kxjhlele.iteye.com/blog/323657 以前在javaeye看到过关于Java操作文件的一篇文章,写的很好,但找了半天也没找到,就把找到底几篇文章整理一下 ...

  7. iOS应用架构浅谈

    (整理至http://www.cocoachina.com/ios/20150414/11557.html) 缘由 从事iOS工作一年多了,主要从事QQ钱包SDK开发和财付通app维护,随着对业务的慢 ...

  8. 数据结构(树链剖分):BZOJ 4034: [HAOI2015]T2

    Description 有一棵点数为 N 的树,以点 1 为根,且树点有边权.然后有 M 个 操作,分为三种: 操作 1 :把某个节点 x 的点权增加 a . 操作 2 :把某个节点 x 为根的子树中 ...

  9. 【行业干货】ASOS:外来快时尚品牌的入华战 - 行业干货 - 京东内部论坛 - Powered by Discuz!

    [行业干货]ASOS:外来快时尚品牌的入华战 - 行业干货 - 京东内部论坛 - Powered by Discuz! [行业干货]ASOS:外来快时尚品牌的入华战

  10. Wireless Password - HDU 2825(ac自动机+状态压缩)

    题目大意:有个人想破解他邻居的密码,他邻居告诉了一些关于这个密码的信息,并且给他一个单词集合,他用这些信息判断一下最少有多少种密码. 1->, 所有的密码都是有小写字母组成. 2->,密码 ...