Nested loop in a single table

  • I have a single table that is part of a file management system. The table I'm working with holds all the the folder names and the number assigned to the folder. It also shows the number assigned to the folders parent folder. I have no idea how to even start this. I need to start at the top level and find the first level children folders buy looking up what folders have what parents. After I find that I then need to find what child folders those child folders have by doing the same thing. So I guess I need to get the results of the first search then loop through those results then loop through results. Like I say I know Idea how to even get started. I don't mind reading, I just need point in the right direction.

    Thanks

    -Phillip

  • Start with telling us what you want to do (not how you think you should be doing it). If you could script the table definition and post that here, it would be a big help also.

    [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]

  • phillip.hess (10/29/2008)


    I don't mind reading, I just need point in the right direction.

    Thanks

    -Phillip

    Check out 'Expanding hierarchies' in Books Online. If you mess around with the example you might have some light bulbs suddenly appear.

    Greg
    _________________________________________________________________________________________________
    The glass is at one half capacity: nothing more, nothing less.

  • Currently I use a very simple query:

    SELECT o_projectno,o_projectname

    FROM dms_proj

    WHERE o_parentno = 0

    ORDER BY o_projectno

    and I combine all three quereies by hand. What I need to end up with is shown in SQL_04.png. The image file SQL_03.png shows the workflow involved in getting to what I need. I'm unable to place any more of the data then I already have or I would place a small sample of the table.

  • This is nice, but it is not what was asked for.

    Here is a link that explains how to get better results from these forums: http://www.sqlservercentral.com/articles/Best+Practices/61537/.

    Here are the things that would be really useful, for us to help you, in order of importance:

    1) Please describe the rowset/dataset that you would like to have returned. SQL returns data in rowsets, if you can describe the dataset that you want SQL to give to you, we probably can help you.

    2) Script the source table's definition out and paste that text here. Please do not post images or screenshots, they have extremely low usefulness to us.

    3) Provide us with some sample data, in the form of INSERT statements.

    Things that we do not really need:

    A) Another statement of the Problem: You did that fine the first time, and we do understand it. We know what the "linked-parent" hierarchy design is and we know that it can be confusing, especially when trying to write a query for it. However, we cannot tell you much about the solution to your problem that will not seem vague and even more confusing without some of the information above.

    B) How you are trying to solve the Problem: We understood that too. However, the nested-Cursors approach to this problem, although natural and easy to understand, is usually a terrible solution in SQL Server.

    [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 5 posts - 1 through 4 (of 4 total)

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