我认为您必须为每项任务进行更新。这可以解决问题(语法适用于 SQL Server):
DECLARE @TaskID int
DECLARE @SubTaskPosition int
DECLARE curSubTaskPositionUpdate cursor fast_forward
FOR
SELECT TaskID, SubTaskPosition
FROM TaskDetails
WHERE SubTaskID = @SubTaskID
OPEN curSubTaskPositionUpdate
FETCH NEXT FROM curSubTaskPositionUpdate INTO @TaskID, @SubTaskPosition
WHILE @@FETCH_STATUS = 0
BEGIN
UPDATE TaskDetails
SET SubTaskPosition = SubTaskPosition - 1
WHERE TaskID = @TaskID
AND SubTaskPosition > @SubTaskPosition
FETCH NEXT FROM curSubTaskPositionUpdate INTO @TaskID, @SubTaskPosition
END
CLOSE curSubTaskPositionUpdate
DEALLOCATE curSubTaskPositionUpdate
请注意,您在从 TaskDetails 删除之前执行此操作...并且您可能希望将所有内容包装在事务中。
Gordon Edit - 我必须在更新代码中包含我的删除代码才能使其工作,否则(a)删除首先发生并且“subtaskID = @subtaskID”没有返回任何内容,或者(b)我在重新执行后删除了订购和重新订购(正确!)没有效果。
DECLARE @TaskID uniqueidentifier
DECLARE @SubTaskPosition int
DECLARE curSubTaskPositionUpdate cursor fast_forward
FOR
SELECT TaskID, SubTaskPosition
FROM TaskDetails
WHERE SubTaskID = @subTaskID
OPEN curSubTaskPositionUpdate
FETCH NEXT FROM curSubTaskPositionUpdate INTO @TaskID, @SubTaskPosition
WHILE @@FETCH_STATUS = 0
BEGIN
-- Delete the subTask
DELETE FROM TaskDetails
WHERE TaskID = @TaskID
AND SubTaskPosition = @SubTaskPosition
-- Update the other subTasks
UPDATE TaskDetails
SET SubTaskPosition = SubTaskPosition - 1
WHERE TaskID = @TaskID
AND SubTaskPosition > @SubTaskPosition
FETCH NEXT FROM curSubTaskPositionUpdate INTO @TaskID, @SubTaskPosition
END
CLOSE curSubTaskPositionUpdate
DEALLOCATE curSubTaskPositionUpdate