这个不大好表达,举个例子:
表tA:
id(自增主键) 考试时间 姓名 数学 语文 英语
1    2010-1-1    a    80    90    90
2    2009-8-1    b    70    80    90
3    2009-12-1    b    80    80    70
4    2010-7-1    c    80    60    100我想把这个表里的数据按照[考试时间]的倒序,依次插入到一个临时表中,形成一个这样的:
sn id 考试时间 姓名 数学 语文 英语
1    4    2010-7-1    c    80    60    100
2    1    2010-1-1    a    80    90    90
3    3    2009-12-1    b    80    80    70
4    2    2009-8-1    b    70    80    90
主要是因为想实现存储过程翻页,按照时间倒序排列,所以用了下临时表。
我用了下面的语句:
select identity(int,1,1) sn, id, 考试时间, 姓名, 数学, 语文, 英语
from tA
order by 考试时间 desc但是sql执行的插入是随机性的,并不是按照order by 的顺序插入的,只是插入后按照order by又排了顺序,可sn是乱的。请高手们指教!

解决方案 »

  1.   

    select identity(int,1,1) sn, id, 考试时间, 姓名, 数学, 语文, 英语
    into #临时表 
    from tA
    order by 考试时间 desc这样SN不会是乱的吧?
      

  2.   

    creat table #t
    (
    sn  identity (int,1,1),
    id  int,
    .........
    )
    insert into #t(id,......)
    select id,.......  from ta order by XXX   desc
    select *  from #t
      

  3.   

    --------------------------------------------------------------------------
    --  Author : htl258(Tony)
    --  Date   : 2010-04-01 08:54:42
    --  Version:Microsoft SQL Server 2008 (SP1) - 10.0.2531.0 (Intel X86) 
    --          Mar 29 2009 10:27:29 
    --          Copyright (c) 1988-2008 Microsoft Corporation
    --          Developer Edition on Windows NT 5.1 <X86> (Build 2600: Service Pack 2)
    --------------------------------------------------------------------------
    --> 生成测试数据表:tbIF NOT OBJECT_ID('[tb]') IS NULL
    DROP TABLE [tb]
    GO
    CREATE TABLE [tb]([id] INT primary key,[考试时间] DATETIME,[姓名] NVARCHAR(10),[数学] INT,[语文] INT,[英语] INT)
    INSERT [tb]
    SELECT 1,N'2010-1-1','a',80,90,90 UNION ALL
    SELECT 2,N'2009-8-1','b',70,80,90 UNION ALL
    SELECT 3,N'2009-12-1','b',80,80,70 UNION ALL
    SELECT 4,N'2010-7-1','c',80,60,100
    GO
    --SELECT * FROM [tb]-->SQL查询如下:select sn=IDENTITY(INT),* into # from tb order by 考试时间 descselect * from #
    /*
    sn          id          考试时间                    姓名         数学          语文          英语
    ----------- ----------- ----------------------- ---------- ----------- ----------- -----------
    1           4           2010-07-01 00:00:00.000 c          80          60          100
    2           1           2010-01-01 00:00:00.000 a          80          90          90
    3           3           2009-12-01 00:00:00.000 b          80          80          70
    4           2           2009-08-01 00:00:00.000 b          70          80          90(4 行受影响)
    */这样不对吗
      

  4.   

    对啊!逻辑上来说,应该是先ORDER BY 在SELECT才是对的啊!
      

  5.   

    /*------------------------------问题描述----------------------------------------*如何按insert的先后排序存储数据??????????*现在有一张表,希望能够按数据操作时的insert顺序排序,*比如:最先insert的排在前面,后insert的排在后面 */1:如果表中有聚集索引,那么是不可能实现的,因为数据会自动按照聚集索引排序 2:如果表中没有聚集索引,数据表是以堆集结构存储的。和你的默认字符集又关联.数据库存放和取出的顺序没有一定的规律的,可能两次取出的顺序都不一样。  --这里解决方法:加一個時間字段,設定default  value  = getdate(),建一个clustered索引,以这个字段为第一个字段。 create table test (value int,n int) insert test values (1,2) insert test values (3,3) insert test values (2,4) --添加默认字段 alter table test add [datetime] datetime default getdate() --为该字段加聚集索引 create clustered index cl_in_date on test([datetime])
      

  6.   

    楼主的这个需求 我看很难实现
    select into order by 不能确定插入顺序(微软文档没说明)
    最好是插入后查询的时候排序 
      

  7.   

    这是微软在KB文章中说的解决办法。楼主的方法是不能保证SN是按照ORDER BY 的顺序的。
      

  8.   

    http://support.microsoft.com/?scid=kb%3Ben-us%3B273586&x=5&y=7
    http://blog.csdn.net/misterliwei/archive/2010/01/05/5134043.aspx
      

  9.   

    [code=select identity(int,1,1) as 'sn',a.* into #t from ( select top 10000000 * from ta order by 考试时间 desc) a
    ---top 10000000  只要比你所有的记录条数大就可以了][/code]
      

  10.   


    select identity(int,1,1) as 'sn',a.* into #t from ( select top 10000000 * from ta order by 考试时间 desc) a
    ---top 10000000  只要比你所有的记录条数大就可以了
      

  11.   

    感谢大家这么热情的回复,咱们CSDN还是人才济济啊,根据大家的思路问题解决了,是这样的:
    1、创建临时表(有自增主键);
    2、Insert into(主要数据列..) 临时表 Select 主要数据列.. From 实际表 Order by 排序列。结贴散分!多谢!