要求按照规定排序顺序,查找表中第m行和第n行的记录,因此将所查询所有数据into到临时表中:select * into #table1 from XXX where XXX order by XXX,然后再给表加 idx INT IDENTITY,再根据idx值,取出数据。where idx between m and n。执行无误。共有多个此类问题,都这样实现的。可是测试时,先执行完table1的查询后,再执行table2的查询,却发现排序顺序不正确,这两个局部临时表的名字不一样,也没有谁先谁后的必然联系,按说不应该有什么影响啊,也不知道是什么原因,请大家帮帮忙阿。

解决方案 »

  1.   

    这样取取试试:
    select id=identity(int,1,1),* into #table1 from XXX where XXX order by XXX
      

  2.   

    查询信息是按照classname,studentcode组和排序的,使用id=identity(int,1,1)时,查询就自动按照studentcode来排序set id了,不是按照顺序由1加1,set id的。
      

  3.   

    SELECT  *  into #Term FROM XX where XX order by XX
    ALTER TABLE #Term ADD idx INT IDENTITY 
    select * from  #Term A where A.idx between 1 and 50
    drop table #Term另一个除了临时表的名字是#Mid,查询的信息也不一样,也是这个步骤。因原查询信息是多个视图INNER JOIN,非常繁琐,暂时就用XX代替一下。问题就是,执行第一个再执行第二个的时候,第二个的排序就会出错,然后再操作第二个,第二个就会正常,。
      

  4.   

    select top 5 ... from xxx
    where x in (select top 10... from xxx where ...)
      

  5.   

    select identity(int,1,1) as OID,* 
    into #temp
    from tabelname
    order by xxxselect top n-m * from #temp
    where OID not in(select top m * from #temp)drop #temp---查询从第m行起到第n行的记录
      

  6.   

    在sqlserver中,是否建立多个临时表就会有冲突阿。就是select * into 后面的临时表的时候,select的排序就会受到上一个的影响?
      

  7.   

    select top (n - m )  * 
    from
    (select top n from table order by  id desc) a
      

  8.   

    那如何在查询的记录集的第一列上,添加序列号阿.比如:
    51,a,b,c
    52,a,b,c
    ......
    100,a,b,c
    由m到n。
      

  9.   

    select identity(int,1,1) as OID,* 
    into #temp
    from tabelname
    order by xxxselect identity(int,50,1),top n-m * from #temp
    where OID not in(select top m * from #temp)drop #temp