这个好办 你的主键是 id 就是那个自增的字段,你准备加的字段和tid
declare @min int ,@max int
update 表 set tid = 0 where 状态 = 1
Select @min = min(id), @Max = Max(id) From 表 Where 状态 = 1
while @min <= @max
begin
update 表 set tid = tid + 1 where id = @Min
set @min = @min + 1
end
如果你想从1到5自己改一下就可以了
alter table t add xxx int identity(1,1)
go
update t set id=(select count(*) from t a where a.xxx<=t.xxx and a.状态=t.状态)
alter table t drop column xxx
go