没有主键的情况:
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
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
select * from #t
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
by id