怎样得到SQL返回结果的第N行?
比如SELECT name FROM person
WHERE name IS NOT NULL结果是:zhao
qian
sun
li
我想返回这个结果的第2行和第3行,即:qian
sun

解决方案 »

  1.   

    SELECT identity(int) id,name into #t FROM person
    WHERE name IS NOT NULLselect top 2 * from #t where id>1
      

  2.   

    SELECT identity(1,1) id,name into #t FROM person
    WHERE name IS NOT NULLselect * from #t where [id] between 2 and 3
      

  3.   

    MS-SQL 2000没这个功能,除非循环再按条件取MYSQL有limit
      

  4.   

    top 2 ?
    ----------------------------
    id>1的头两条
      

  5.   

    如lianqizhi(油条豆腐脑)的 
    SELECT identity(int) id,name into #t FROM person
    WHERE name IS NOT NULLselect top 2 * from #t where id>1要加一个自增字段上去~
      

  6.   

    "The IDENTITY SQL construct or statement is not supported."
      

  7.   

    就一个COLUMN吗?有没有按什么排序呢?如果没有的挂基本上是不能可能做到的!
      

  8.   

    --插入临时表
    select name into #t from  person
    WHERE name IS NOT NULL
    --给临时表添加id
    alter table #t add id identity(1,1)
     
    declare @i int,@k int
    --@i 返回的开始行,@k返回的结束行
    declare @sql varchar(8000)set @i=20
    set @k=30
    SET @sql=isnull(@sql,'')+'select top '+cast(@k-@i as varchar(20))+' name from  #t where id not in(select top '+cast(@i as varchar(20)) +' id from #t)'
    print @sql
    exec(@sql)
      

  9.   

    IDENTITY 的确是可以很简单的解决这个问题,但就是执行前有一个警告:不支持。
      

  10.   

    select * from (select top 3 name from person) a 
    where name not in (select top 1 name from person)
      

  11.   

    select top 1 name from (
    SELECT top 3 name FROM person
    WHERE name IS NOT NULL
    order by name desc)
      

  12.   

    楼主的是什么系统啊?
    identity不支持?
      

  13.   

    select * from (select *, row_number() over(order by getdate()) rn from person) a where rn>=2 and rn<=3
      

  14.   

    试试我的
    declare   @t   table(A1   varchar(10),A2   int)   
      insert   @t   select   'a',1   
      union   all   select   'b',2   
      union   all   select   'c',3   
      union   all   select   'd',4   
      union   all   select   'f',5   select * from @t a where (select count(*) from @t where a2<=a.a2)>=2
    and (select count(*) from @t where a2<=a.a2)<=3 
      

  15.   

    无需临时表,麻烦!
    select top 2 name from person where id in(select id from person
    WHERE name IS NOT NULL) order by id desc
      

  16.   

    select top (1) from b where 
    zhujian not in select top(n) from b
      

  17.   

    通用格式
    返回从n行开始的m条记录
    select top m 列名
    from 表
    where 列名' not in 
    (select top n  列名'
    from 表 
    order by 列名' )
    列名和列名'可以相同,看你用需要返回的列排序还是其他列排序了
      

  18.   

    两种方法
    一、直接新建个临时表
    二、采用top,然后用not in去出重复的方法
      

  19.   

    如果你经常使用的话,加一列比较好.
    不经常使用的话,用select top 上面举的方法挺好.
    临时表也不错(数据量不大的话).