I guess I am not quite understanding the JOIN in recursive CTEs.
I have a table called FAMILIES with columns FAMILYID, MEMBERID, ROLE (wich can be 'F' for father, 'M' for mother, 'C' for child).
I want to return all ancestor IDs (with a level) for a given person ID. Using a scalar function to get the person's FAMILYID, I am trying something like the following (just going for fathers for now):
CREATE PROCEDURE [dbo].[GetAncestors]
(@ID int)
AS
WITH Father(ParentID,Level)
AS
(
SELECT MEMBERID, 0 FROM dbo.FAMILIES
WHERE FAMILYID = dbo.GetParentFamily(@ID) AND ROLE = 'F'
UNION ALL
SELECT F.MEMBERID, Father.Level + 1 FROM dbo.FAMILIES F
INNER JOIN Father ON Father.ParentID = ??
WHERE F.FAMILYID = dbo.GetParentFamily(Father.ParentID) AND F.ROLE = 'F'
)
SELECT * FROM Father
But I don't know what to put in the join. And I don't know how to stop the recursion when I reach an ancestor with an ID of 0. And I don't know how to get both father and mother at each level.
Any advice would be gratefully received.
Jon
Jon Stranger