那就根据主键排序。如表:T(a PK,b varchar)select id=(select count(*) from t where a<=a.a),*
from t a
order by a

解决方案 »

  1.   

    --如果有主键/唯一键(或者是不重复的字段),可以依据这些字段来生成--升序
    select 序号=(select count(*) from 表 where 主键<=a.主键),* from 表 a order by 主键--降序
    select 序号=(select count(*) from 表 where a.主键<=主键),* from 表 a order by 主键 desc
    --但这样的方法,在数据量大时,效率是很低的
      

  2.   

    --如果不用identity,也不用临时表,我想应该就只有上面的低效且有限制的方法--如果不用identity,但可以用临时表,可以这样处理--生成临时表
    select 序号=0,* into #t fro 表 order by ... --可以是按任意排序方式或者是不排序--生成序号
    declare @i int 
    set @i=0
    update #t set @i=@i+1,序号=@i---显示结果
    select * from #t--删除临时表
    drop table #t