I'm not sure how to phrase this. I feel like I have a pretty good understanding of SQL but I'm stumped.
Bit of background.
I have a table CustomersGroups on that table exists GroupName, GroupId, ParentGroupId, ObjectRowState
Customers have the ability to create a Tree Group structure.
parent
-Child
- - GrandChild
- - GreatGrandChild
etc.
If a user deletes a group it deletes all the children associations with it. I want to be able to find a group. Then Use that Group in a Query as the Parent Group ID for its Children so on so forth..
So
SELECT *
FROM tbl_CustomersGroups
WHERE CompanyId = 123
And GroupName = North
Results to GroupID = 111
SELECT *
FROM Tbl_CustomersGroups
WHERE ParentGroupID = 111
results to the Children GroupID 222,333,444
I then want to keep this query going over and over until there is no more groups left. Usually 6 levels deep.
Instead of separate queries over and over, I want one big query where I can put in the ParentGroupID and it will loop down and give me all the children.
I am not asking for a query to be written, I am asking for Guidance or examples as to how to go about this.