Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8254501
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T01:12:44+00:00 2026-06-08T01:12:44+00:00

In my database I have three tables as follows: Tasks (TaskID, TaskName, TaskDescription) TaskDetails

  • 0

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:

  • TaskDetails links Tasks to the constituent SubTasks.
  • A SubTask may be referenced in a number of different Tasks.
  • A Task should be made up of an ordered list of SubTasks… 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 = @subTaskID
  • DELETE 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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T01:12:47+00:00Added an answer on June 8, 2026 at 1:12 am

    I think you’ll have to do an update for each task. This would do the trick (syntax is for 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
    

    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.

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my database I have three tables: Users: UserID (Auto Numbering), UserName, UserPassword and
I have three tables in a Mysql database - countries, cities and hotels. Their
I have three database tables: users emails invitations Emails are linked to users by
I have three mysql table from same database Db1. Three tables have following columns.
I have three or four tables in a MySQL database associated with an upcoming
I have a database with three tables: user_table country_table city_table I want to write
I have a database with three tables in my database namely catering, room, room_booking
I have 3 tables in my SQL database as follows Users2 UserID, EID, Name,
I have a database containing three tables: practices - 8 fields patients - 47
I have a common database joining situation involving three tables. One table, A, is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.