I have managed to build a recursive query which returns rows for the selected Id and all its children. This works absolutely fine for the ultimate parent, but I need it also to work correctly when the passed Id is that of one of the children, showing just the child and its children if any. Currently it still returns other child rows of the ultimate parent plus the passed child row displays twice...
As with a previous issue I have to do this using the sub-query format, because other TSQL based database engines than SQL Server may be used that do not support CTE or the WITH clause.
Desired Outcome:
Using Id 2, the correct data is returned: 2, 3, 4, 6, 7. Using Id 6, it should return 6, 7 only. Currently the query returns 6,3,4, 6,7.
Data:
ProjectId ProjectName ParentId
1 Test Project -1
2 Test Project 2 0
3 Test Project 2 Sub Project 1 2
4 Test Project 2 Sub Project 2 2
5 Test Project 3 -1
6 Test Project 2 Sub Sub Project 1 3
7 Test Project 2 Sub Sub Sub Project 1 6
Query:
DECLARE @PROJECTID BIGINT = 2;
SELECT *
FROM
(
SELECT *
FROM ProjectCostingProjects pcp
WHERE pcp.[ProjectId] = @PROJECTID
UNION ALL
SELECT pcp2.*
FROM ProjectCostingProjects pcp2
JOIN ProjectCostingProjects pcp
ON pcp2.ParentID = pcp.ProjectId
);
Any advice or suggestions gratefully received.
Here is an answer that uses a while loop instead.
Solution 1, using while loop and a temp table
Solution 2 For performance you can create a intermidate table with the result from the while loop. This intermidiate table can be updated on an intervall, or as part of the creating an element in the main table. This could be either a part of your business logic or as a DB trigger.
Here is the code I would write if you wanted this as a scheduled job:
The big advantage of solution #2 is that the performance should be REALY good for reads, probably even better than CTE's. Also it works equally well for reading from bottom to top, as top to bottom.