我有一查询的存储过程,查询出32条记录按升序排列,我如何在查出的32条记录中再读取出其中的一部分记录,比如第11到20的10条记录。在存储过程中实现

解决方案 »

  1.   

    取n到m条记录的语句1.
    select top m * from tablename where id not in (select top n id from tablename)2.
    select top m * into 临时表(或表变量) from tablename order by columnname -- 将top m笔插入
    set rowcount n
    select * from 表变量 order by columnname desc3.
    select top n * from 
    (select top m * from tablename order by columnname) a
    order by columnname desc
    4.如果tablename里没有其他identity列,那么:
    select identity(int) id0,* into #temp from tablename取n到m条的语句为:
    select * from #temp where id0 >=n and id0 <= m如果你在执行select identity(int) id0,* into #temp from tablename这条语句的时候报错,那是因为你的DB中间的select into/bulkcopy属性没有打开要先执行:
    exec sp_dboption 你的DB名字,'select into/bulkcopy',true
    5.如果表里有identity属性,那么简单:
    select * from tablename where identitycol between n and m 6.如果是sql server 2005 可以这样写: 
    select top 20 * from T order col
    except
    select top 2 * from T order col
      

  2.   

    查询第X页,每页Y条记录最基本的处理方法(原理):如果表中有主键(记录不重复的字段也可以),可以用类似下面的方法,当然y,(x-1)*y要换成具体的数字,不能用变量:select top y * from 表 where 主键 not in(select top (x-1)*y 主键 from 表)如果表中无主键,可以用临时表,加标识字段解决.这里的x,y可以用变量.select id=identity(int,1,1),*  into #tb from 表
    select * from #tb where id between (x-1)*y and x*y-1
      

  3.   

    存储过程alter一下,增加两个参数,一个起始页,一个终止页,然后参考楼上几位的top方法,应该就差不多了。
      

  4.   

    select top 10 * from (select top 30 * from (select top 32 * from tb order by ID asc)T)TT order by ID DESC
      

  5.   

    select top 10 *
    from 
    (
    32条记录
    )T
      

  6.   

    select top n * from 
    (select top m * from tablename order by columnname) a
    order by columnname desc