设为首页收藏本站

Scripts 学盟

 找回密码
 加入学盟

QQ登录

只需一步,快速开始

查看: 1002|回复: 2
打印 上一主题 下一主题

SQL SERVER取第几行到第几行的方法 [复制链接]

Rank: 7Rank: 7Rank: 7

跳转到指定楼层
1#
俊俊 实名认证  发表于 2012-10-11 15:48:13 |只看该作者 |倒序浏览

------------------SQL SERVER 2000 取第几行到第几行的方法-------------------

--(第一种方法) 效率最差

select   top   5   *   from   customers   where   CustomerID   not   in   (select   top   5   CustomerID   from   customers)

--(第二种方法)  效率最好, 这里先执行的是Order by 然后才执行Top

select   b.*   from ( select   top   5   a.*   from   ( select   top   10   *   from   customers   order   by   CustomerID   asc )   a  order   by   a.CustomerID   desc ) b order   by   b.CustomerID   

--(第三种方法) 利用临时表和identity(int,1,1

DROP TABLE #temp

select  identity(int,1,1) as rowID,*   into   #temp   from   customers

SELECT * FROM #temp WHERE rowID>5 AND rowID<=10


------------------SQL SERVER 2005+ 取第几行到第几行的方法-------------------

SELECT b.* FROM (SELECT ROW_NUMBER() OVER(ORDER BY c.customerid) rowIndex,* FROM Customers c) b WHERE b.rowIndex>5 AND b.rowindex<=10
分享到: QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
分享分享0 收藏收藏1

管理员

超级大菜鸟

Rank: 9Rank: 9Rank: 9

2#
混混@普宁.中国 实名认证  发表于 2012-10-11 21:44:45 |只看该作者
有 row_numer() 真是爽歪歪呀

使用道具 举报

Rank: 7Rank: 7Rank: 7

3#
俊俊 实名认证  发表于 2012-10-18 11:32:13 |只看该作者
混混@普宁.中国 发表于 2012-10-11 21:44
有 row_numer() 真是爽歪歪呀

科技,以人为本!

使用道具 举报

您需要登录后才可以回帖 登录 | 加入学盟

手机版|Scripts 学盟   |

GMT+8, 2024-5-19 08:38 , Processed in 1.086408 second(s), 11 queries .

Powered by Discuz! X2

© 2001-2011 Comsenz Inc.

回顶部