Recursive SQL Server query

0

Please login or register to vote for this query.

(click on this box to dismiss)

http://stackoverflow.com/questions/13933255/recursive-sql-server-query

Computational Science

Q&A for scientists using computers to solve scientific problems

DECLARE @TableName TABLE(reviewer int, reviewee int);

INSERT INTO @Tablename VALUES
(2,1),
(3, 2),
(4, 3),
(5, 4);

DECLARE @revieweeID INT = 4;
DECLARE @reviewerID INT = 2;


;WITH CTE
AS
(
   SELECT reviewer, reviewee, 0 AS Level
   FROM @TableName 
   WHERE reviewee = @revieweeID
   UNION ALL
   SELECT p.reviewer, p.reviewee, c.Level + 1 
   FROM CTE c
   INNER JOIN @TableName p ON c.reviewee = p.reviewer
)
SELECT * FROM CTE
;

Enter Parameters

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