Solusi yang saya temukan tidak bagus sama sekali. Ini memberikan jawaban yang benar tetapi sangat lambat, bahkan untuk tabel yang sangat kecil ini.
DECLARE @INCLUIDOS TABLE (ID INT)
INSERT INTO @INCLUIDOS VALUES(1)
DECLARE @PAST_QUANT INT = 0
DECLARE @QUANT INT = 1
WHILE @QUANT <> @PAST_QUANT
BEGIN
SET @PAST_QUANT = @QUANT
INSERT INTO @INCLUIDOS
SELECT PARENT
FROM PERSONCONN
WHERE CHILD IN (SELECT ID FROM @INCLUIDOS)
AND PARENT NOT IN (SELECT ID FROM @INCLUIDOS)
INSERT INTO @INCLUIDOS
SELECT CHILD
FROM PERSONCONN
WHERE PARENT IN (SELECT ID FROM @INCLUIDOS)
AND CHILD NOT IN (SELECT ID FROM @INCLUIDOS)
SET @QUANT = (SELECT COUNT(*) FROM @INCLUIDOS)
END
SELECT DISTINCT ID FROM @INCLUIDOS