I have a table with the following schema:
CREATE TABLE Feature
(
FeatureId INT IDENTITY(1,1),
ParentFeatureId INT,
TargetObjectType VARCHAR(64)
)
As you can see, it defines a tree of features. I am interested to collect all the descendants found under the given nodes.
My current solution seems rather awkward to me:
IF OBJECT_ID('tempdb..#features') IS NOT NULL
DROP TABLE #features
SELECT
0 FeatureId, TargetObjectType, ParentFeatureID
INTO
#features
FROM
Feature
WHERE
1 = 0
INSERT INTO #features
SELECT
FeatureId, TargetObjectType, ParentFeatureID
FROM
Feature
WHERE
TargetObjectType IN ('Root1', 'Root2')
WHILE @@ROWCOUNT > 0
INSERT INTO #features
SELECT FeatureId, TargetObjectType, ParentFeatureID
FROM Feature f
WHERE ParentFeatureID IN (SELECT FeatureId FROM #features)
AND NOT EXISTS (SELECT 1 FROM #features WHERE FeatureId = f.FeatureId)
SELECT * FROM #features
Is there a more elegant way of doing it? Using a recursive CTE, maybe?
The volume of data is insignificant (a few thousand records at most). All the data represents a forest with just a few levels of hierarchy, say 7 at the most.