I have this SQL Server table:
I want to execute a query that will return this output:
I tried to use the code from:
Multi-Level parent-child relationship
WITH CTE AS
(
SELECT A.[Value], A.[ValueID], 1 AS [Level]
FROM [dbo].[MyTable] AS A
WHERE A.ParentValueID = 0
UNION ALL
SELECT CTE.[Value], CTE.[ValueID], Level + 1
FROM CTE
INNER JOIN [dbo].[MyTable] AS B ON CTE.[ValueID] = B.[ValueID]
)
SELECT * FROM CTE
That did not seem to work as I got an error:
The statement terminated. The maximum recursion 100 has been exhausted before statement completion.