我有一列(TaskPosition),用于跟踪html表中的记录位置。至关重要的是,即使有删除,此列中的值也应保持数字顺序无间隙。
TaskID TaskName TaskPosition
1 Project Planning 1
2 Define Requirements 2
3 Develop Options 3
4 Finalise Solution 4
如果我删除TaskPosition = 3的记录,那么我希望TaskPosition列为
TaskID TaskName TaskPosition
1 Project Planning 1
2 Define Requirements 2
4 Finalise Solution 3
答案 0 :(得分:1)
在期间使用
ROW_NUMBER()
表达式生成整数SELECT
@Damien_The_Unbeliever
create table #noGapInt (TaskID int identity(1,1), TaskName varchar(10), TaskPosition int)
insert #noGapInt (TaskName , TaskPosition) values ('a',1),('a',2),('a',3),('a',4),('a',5),('a',6)
select * from #noGapInt
delete from #noGapInt where TaskPosition = 4
select * from #noGapInt
select *, ROW_NUMBER() over(order by TaskID) TaskPosition from #noGapInt