Recursive Insertion using SQL 2005

  • Hi,

    I have a task where in i will send the ID in the below example ID=1

    and i need to make a exact copy in a similar fashion for all the sublevels it has. I have given an example in which way it should happen, please guide me to acheive this.

    ========================================================================

    IDName ParentID RootID

    ========================================================================

    1Chief Executive Officer11

    2Senior Director - Development11

    3Senior Director - Finance11

    4Senior Director - Human Resources11

    5Product Development Manager21

    6Project Lead51

    7QA Lead51

    8Documentation Lead51

    9Developers 61

    10Testers71

    11Writers81

    12Accountants31

    13HR Professionals 41

    The above values need to get replicate as bellow

    ========================================================================

    IDName ParentID RootID

    ========================================================================

    101Chief Executive Officer101 101

    102Senior Director - Development101 101

    103Senior Director - Finance101 101

    104Senior Director - Human Resources101 101

    105Product Development Manager102 101

    106Project Lead105 101

    107QA Lead105 101

    108Documentation Lead105 101

    109Developers 106 101

    110Testers107 101

    111Writers108 101

    112Accountants103 101

    113HR Professionals 104 101

    Please help in how to achieve this.

    With Regards

    Dakshina murthy

  • This should work:

    INSERT into YourTable( ID, [Name], [ParentID], [RootID] )

    Select Id+100, [Name], PaerntId+100, RootID+100

    From YourTable

    Where RootID = 1

    Note: Not tested, as no DDL or sample data was provided.

    [font="Times New Roman"]-- RBarryYoung[/font], [font="Times New Roman"] (302)375-0451[/font] blog: MovingSQL.com, Twitter: @RBarryYoung[font="Arial Black"]
    Proactive Performance Solutions, Inc.
    [/font]
    [font="Verdana"] "Performance is our middle name."[/font]

Viewing 2 posts - 1 through 1 (of 1 total)

You must be logged in to reply to this topic. Login to reply