已有一张表,我想在表里增加一个自动增长列,但是我想添进去的自动增长列按关键字order by排序,有简单易行的方法吗
RT
------解决方案--------------------select id=identity(int,1,1),* into newtbname from oldtbname order by 关键字 
------解决方案--------------------select NewID = identity(int,1,1) into NewTable from OldTable order by 旧的数据的编号按关键字排序
------解决方案--------------------上面漏了 * 
 select NewID = identity(int,1,1), * into NewTable from OldTable order by 旧的数据的编号按关键字排序
------解决方案--------------------只是提供一个思路,并不是考试答题,你可以用临时表,清空原表,改好表结构,再Insert,并非一成不变。
------解决方案--------------------用2005的排序函数 row_number() 
 select row_number() over (order by id), * from sysobjects 
------解决方案--------------------就是先给你关键字建一个有序列的索引 
 然后再在你的表的增加一个自动增长列如ID 
 ------ 
 ---创建测试环境 
 Create Table 测试ADD(Name Varchar(8),Class int) 
    Insert 测试ADD Select  'ddd ',1 
    Union All Select  'bbb ',3 
    Union All Select  'ccc ',2 
    Union All Select  'aaa ',4 
 Select * From 测试ADD 
 /*假设你的关键字为:Class 
   你要对Class排序建自增列 
 */  
 如: 
 ---1、创建索引 
 Create Nonclustered Index Test_Index 
 On 测试ADD(Class Asc) 
 With 
 Fillfactor=50 
 ---2 增加id列,id列为自增列 
 Alter Table 测试ADD Add id int identity(1,1)   
 ---3、删除索引 
 Drop Index 测试ADD.Test_index  
 ---4、查询结果 
 Select * From 测试ADD     
 /*注意下面结果 
   自增列id 为   1 3 2 4 
   并不是        1 2 3 4 
   (说明它是按照Class升序自增的) 
 */   
 /* 
 Name     Class       id           
 -------- ----------- -----------  
 ddd      1           1 
 bbb      3           3 
 ccc      2           2 
 aaa      4           4   
 (所影响的行数为 4 行)   
 */
------解决方案--------------------楼上正确 
 补充几点: 
 > 1  
 Create Nonclustered Index IndexName 
 On(按楼主需要可以是多个字段)    
 > 2   
 查看时select id,按楼主需要可以是多个字段 from 表 order by 按楼主需要可以是多个字段