没有主键的情况:
select identity(int,1,1) as XH, * into #t from users
go
select * from #t order by XH
go
有主键的情况:
select (select count(*) from users where TID <= A.TID) as XH, * from users as A
 order by TID

解决方案 »

  1.   

    用临时表:select 记录号=identity(int,1,1),* into #t from 表
    select * from #t
      

  2.   

    有主键(不重复的字段都可以)的时候:select 记录号=(select sum(1) from 表 where 主键<=a.主键),* from 表 a
      

  3.   

    查询时加序号:
      a:没有主键的情形:
       Select identity(int,1,1) as iid,* into #tmp from TableName
       Select * from #tmp
       Drop table #tmp
      b:有主键的情形:
       Select (Select sum(1) from TableName where KeyField <= a.KeyField) as iid,* from TableName a
      

  4.   

    select (select sum(1) from table1 where id<=A.id) as xh, * from table1 as A order 
    by id