浅淡SqlServer的Top与Oracle的RowNum

原创
2016-06-07 15:51:38 738浏览

欢迎进入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

  

  第二种写法:

  select top 10 * from UserInfo where UserId not in

  (select top 10 UserId from UserInfo )

  

  第三种写法:

  select top 10 * from UserInfo where UserId>

  (select max(UserId) from

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

  

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

  select * from (select Row_Number() over

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

  where U.RowId between 10 and 20

  

[1] [2]

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。