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

  • Home
  • SEARCH
  • 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 4324646
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T09:09:38+00:00 2026-05-21T09:09:38+00:00

I have a table containing typical hierarchical data for tasks with an arbitrary level

  • 0

I have a table containing typical hierarchical data for tasks with an arbitrary level of subtasks. ParentID is NULL for root level tasks.:

CREATE TABLE [dbo].[tblTask](
    [TaskID] [int] IDENTITY(1,1) NOT NULL,
    [TaskDescription] [nvarchar](255) NOT NULL,
    [TaskStatusID] [int] NOT NULL,
    [ParentID] [int] NULL
)

At the beginning of each month, I need to duplicate the hierarchy with new TaskID‘s for each task that isn’t complete (TaskStatusID == Complete), then close all the original tasks. To save myself grief my first inclination would be to tackle this in c# with which I have greater proficiency than SQL but I would like to first try to understand if there is a good way to tackle this directly in the database.

Update: @Abe I’m not sure what sample data you need particularly, and there is no desired output. I need to duplicate the structure in the table but with new TaskID‘s. This is for SQL Server.

@thursdaysgeek assume that if a parent task is complete then all sub-tasks are also complete. And the rule is that if the sub-tasks of a root task are all complete then I can set the root task to complete. Otherwise if a parent task is not complete but the child task is then I need to only duplicate the parent and not the child. Hope that helps.

  • 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-05-21T09:09:39+00:00Added an answer on May 21, 2026 at 9:09 am

    One way would be to modify your table structure and add something like a CopiedFromTaskID column that is normally NULL.

    Each month you copy all the rows for each task that isn’t complete and while inserting these new rows you also update the CopiedFromTaskID column to be the ID of the task from which each row was copied. This lets you tie the new row back to the row from which it was copied.

    INSERT INTO
        tblTask (TaskDescription, TaskStatusID, ParentID, CopiedFromTaskID)
    SELECT  TaskDescription, TaskStatusID, ParentID, TaskID
    FROM    tblTask
    WHERE   TaskStatusID <> Complete    --Note pseudo code here
    

    Next, you run SQL to alter the ParentId of these newly-inserted rows. Since you have the CopiedFromTaskID you can use that to update the ParentID to reflect the new value, as in this SQL:

    UPDATE
        tblTask
    SET
        tblTask.ParentID = InlineTable.NewTaskID
    FROM
        tblTask INNER JOIN
        (
            SELECT  TaskID AS NewTaskID,
                    CopiedFromTaskID AS OldTaskID
            FROM    tblTask
            WHERE   CopiedFromTaskID IS NOT NULL
        ) AS InlineTable ON tblTask.TaskID = InlineTable.OldTaskID
    WHERE
        tblTask.CopiedFromTaskID IS NOT NULL
    

    Lastly, you update the table one more time to make all the CopiedFromTaskID values NULL so it will be ready for the next time you run it:

    UPDATE
        tblTask
    SET CopiedFromTaskID = NULL
    WHERE   CopiedFromTaskID IS NOT NULL
    

    You’d want to run all these steps inside a transaction in a stored procedure, but it accomplishes what you want without cursors/loops and within the database. You would need to throw in the SQL statement to “close” all the original tasks as well, obviously.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have table containing data . In every row there is a checkbox plus
I have a table containing data and one of the fields is 'class' When
I have a table containing prices for a lot of different things in a
I have a table containing the runtimes for generators on different sites, and I
I have a table containing hundreds of entries and I am trying to delete
I have a table containing cells with text of various lengths. It is essential
I have a MySQL table containing domain names: +----+---------------+ | id | domain |
I have the following table containing the winning numbers of 6/49 lottery. +-----+------------+----+----+----+----+----+----+-------+ |
Say I have at database table containing information about a news article in each
We have a SQL Server table containing Company Name, Address, and Contact name (among

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.