SQL Server,怎样select从第n条开始的10条记录??

qdzhulf 2001-10-25 03:46:16
在PostgreSQL中,可用下述语句:
select * from db start 5,10 order by id;
/*可能是吧?现在不用PostgreSQL了,记不确切了*/
来选取从第5条开始的10条纪录。
请问在SQL Server中有“start n,n+10”类似的语句吗?
谢了
...全文
349 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
nashan 2001-12-07
  • 打赏
  • 举报
回复
这个问题在数据库版里已经有人问过了,我给出的解答是:
select top n * into #temp from db
select top 10 * from db where id not in
(select id from #temp)
其中temp为临时表。

如想选从n开始的m条记录(M为参数,可在过程中赋值)
则可如下执行:
declare @pageno int
set @pageno=m
set rowcount @pageno
select top n * into #temp from db
select * from db where id not in --此处*不可改为top @pageno(可以试一下)
(select id from #temp)
set rowcount 0
nashan 2001-12-07
  • 打赏
  • 举报
回复
这个问题在数据库版里已经有人问过了,我给出的解答是:
select top n * into #temp from db
select top 10 * from db where id not in
()
gxdq 2001-12-05
  • 打赏
  • 举报
回复



看吧你们累的,用临时表不就得了吗?



ghkong 2001-11-16
  • 打赏
  • 举报
回复
select top 10 * from tab1 as aa where aa.field not in (select top 5 field from tab1 ) 有致命的错误:
如果field内容不是唯一的,那么取出的就没有足够的行数。

lianghu 2001-11-12
  • 打赏
  • 举报
回复
同意楼上的,不过楼顶的似乎搞反了:
select top 10 * from tab1 as aa where aa.field not in (select top 5 field from tab1 )
知足常乐 2001-11-12
  • 打赏
  • 举报
回复

我觉得第一个人:argin(猫眉毛) 回答得很对嘛

干吗还不揭贴????????????????
guoan_fox 2001-11-10
  • 打赏
  • 举报
回复
关于这个问题我又连三种方法请参考:
1:游标法(cursor):
delcare @n int,@m int,@icount int
select * into #tmp from tablename
set @icount=1
declare mycursor cursor for
select * from #tmp
open mycursor
fetch next from mycursor
while (@fetch_status=0)
begin
if (@icount<@n) or (@icount>@n+@m-1)
delete from #tmp where current of mycursor
set @icount=@icount+1
end
close mycursor
deallocate mycursor
select * from #tmp
2.通过临时表结构的转变更简单,生成一个临时结构,增加一个字段(identity(1,1)).


nasco 2001-11-09
  • 打赏
  • 举报
回复
都是不懂DB的人搞DB,有这种需求就说明系统设计本身就有问题
ar7_top 2001-11-08
  • 打赏
  • 举报
回复
多给点分数吧
呵呵
小伍老师 2001-11-07
  • 打赏
  • 举报
回复
between选择只是编号5-10的 如果中间被删除一条 你就完完
visc 2001-11-03
  • 打赏
  • 举报
回复
使用pseudo column
例如:
SELECT * FROM emp
WHERE rownum <10;
就是选前十行
kjy12 2001-11-03
  • 打赏
  • 举报
回复
只要用个between 不就行了select * from table name aa_id between '5'and '10'
WxmJun 2001-10-29
  • 打赏
  • 举报
回复
我感兴趣
东土 2001-10-25
  • 打赏
  • 举报
回复
select top 5 * from tab1 as aa where aa.field not in (select top 10 field from tab1 )

34,594

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧