In my database I have three tables as follows:
Tasks (TaskID, TaskName, TaskDescription)TaskDetails (TaskID, subTaskPosition, SubTaskID)Subtasks (SubTaskID, Description)
I am trying to write a Stored Procedure to delete a SubTask completely and then re-order the other SubTasks for a Task.
Things to note:
TaskDetailslinksTasksto the constituentSubTasks.- A
SubTaskmay be referenced in a number of different Tasks. - A
Taskshould be made up of an ordered list ofSubTasks… i.e. 1,2,3,4 NOT 1,3,4,5.
It is fairly easy to delete a SubTask and its links to a Task as follows:
DELETE FROM TaskDetails WHERE SubTaskID = @subTaskIDDELETE FROM SubTasks WHERE SubTaskID = @subTaskID
However, I cannot fathom how to re-order the other SubTasks in the TaskDetails table once the initial SubTask has been deleted. In English, I need to do the following – “For all the Tasks that have just had a SubTask deleted from it, subtract 1 from all of the subTaskPosition fields that occur after where the deleted row used to be”.
Any help or pointers appreciated…
Gordon
I think you’ll have to do an update for each task. This would do the trick (syntax is for SQL Server):
Note you do that before deleting from TaskDetails… and you probably want to wrap everything in a transaction.
Gordon Edit – I had to include my delete code within the update code to make this work, otherwise either (a) the delete occurred first and “where subtaskID = @subtaskID” returned nothing OR (b) I did the delete after the re-ordering and the re-order (correctly!) had no effect.