Home  >  Article  >  Database  >  浅淡SqlServer的Top与Oracle的RowNum

浅淡SqlServer的Top与Oracle的RowNum

WBOY
WBOYOriginal
2016-06-07 15:51:381080browse

欢迎进入Oracle社区论坛,与200万技术人员互动交流 >>进入 平时的项目开发中,分页存储过程是用的比较多的存储过程,SqlServer分页存储过程中经常要用到top,Oracle中则经常用到了RowNum. 现在,有一个UserInfo表,一个字段是UserId,另一个字段是UserName,其中

欢迎进入Oracle社区论坛,与200万技术人员互动交流 >>进入

  平时的项目开发中,分页存储过程是用的比较多的存储过程,SqlServer分页存储过程中经常要用到top,Oracle中则经常用到了RowNum.

  现在,有一个UserInfo表,一个字段是UserId,另一个字段是UserName,其中是UserId是自动增长的,步长是1.表中共有30条数据,其中UserId的值不一定是连续的。现在要实现的目的是取其中的第11至第20条记录。先看SqlServer的几种做法:

  第一种写法:

  select top 10 *

  from UserInfo

  where UserId in

  (

  select top 20 UserId

  from UserInfo

  )

  order by UserId desc

  浅淡SqlServer的Top与Oracle的RowNum

  第二种写法:

  select top 10 * from UserInfo where UserId not in

  (select top 10 UserId from UserInfo )

  浅淡SqlServer的Top与Oracle的RowNum

  第三种写法:

  select top 10 * from UserInfo where UserId>

  (select max(UserId) from

  (select top10 UserId from UserInfo order by UserId) a)

  浅淡SqlServer的Top与Oracle的RowNum

  第四种写法(只可在Sqlserver 2005中):

  select * from (select Row_Number() over

  (Order by UserId) as RowId ,* from UserInfo) U

  where U.RowId between 10 and 20

  浅淡SqlServer的Top与Oracle的RowNum

[1] [2] 

浅淡SqlServer的Top与Oracle的RowNum

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn