您好,欢迎访问一九零五行业门户网

浅淡SqlServer的Top与Oracle的RowNum

欢迎进入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]
其它类似信息

推荐信息