alter table t1 alter column xh int identity(1,1) not null go select * from t1
如果排序的字段唯一,那可以:select (select count(*) from tablename b where b条件 and b.排序字段<=a.排序字段 ) as xh,field1,..... from tablename a where a条件 order by 排序字段 asc 或者: select (select count(*) from tablename b where b条件 and b.排序字段>=a.排序字段 ) as xh,field1,..... from tablename a where a条件 order by 排序字段 desc如果排序的字段不唯一,那只有用 j9988(j9988) 的方法,不过要加排序:select identity(int,1,1) as xh,field1,field2..... into #temp from tablename order by 排序字段 ...select * from #temp order by xh
如果已经排好序 alter table t1 drop column xh alter table t1 add xh int identity(1,1) not null
go
select * from t1
或者:
select (select count(*) from tablename b where b条件 and b.排序字段>=a.排序字段 ) as xh,field1,..... from tablename a where a条件 order by 排序字段 desc如果排序的字段不唯一,那只有用 j9988(j9988) 的方法,不过要加排序:select identity(int,1,1) as xh,field1,field2..... into #temp from tablename order by 排序字段 ...select * from #temp order by xh
alter table t1 drop column xh
alter table t1 add xh int identity(1,1) not null