CREATE TABLE #tbl( ID INT, ParentId INT, Name VARCHAR(...

0

Please login or register to vote for this query.

(click on this box to dismiss)

Ukrainian Language

Q&A for linguists, teachers and students of the Ukrainian language

CREATE TABLE #tbl(
	ID			INT,
	ParentId	INT,
	Name		VARCHAR(30)
);
INSERT INTO #tbl VALUES
(1,		Null,	'Corporate_HQ'),
(2,		1,		'South_Region'),
(3,		1,		'North_Region'),
(4,		1,		'East_Region'),
(5,		1,		'West_Region'),
(6,		3,		'Chicago_District'),
(7,		3,		'Milwaukee_District'),
(8,		3,		'Minneapolis_District'),
(9,		6,		'Gold_Coast_Dealer'),
(10,	6,		'Blue_Island_Dealer');

WITH rCTE AS(
	SELECT *, 0 AS Level FROM #tbl
	UNION ALL
	SELECT t.*, r.Level + 1 AS Level
	FROM #tbl t
	INNER JOIN rCTE r
		ON t.ParentId = r.ID
)
SELECT * FROM rCTE OPTION(MAXRECURSION 0);

DROP TABLE #tbl;

Enter Parameters

Options:
Switch sites:
loading Hold tight while we fetch your results