Say, given an ExceptionRepositoryId, I want to display all its children. After a little while I came up with a recursive SQL:
DECLARE @parentId intAnd I am happy to get the results like the following:
SET @parentId = 210;
WITH TEMP (ExceptionRepositoryId, ParentExceptionRepositoryId)
AS
(
SELECT
ExceptionRepositoryId, ParentExceptionRepositoryId
FROM
dbo.tbl_ExceptionRepository
WHERE
ExceptionRepositoryId = @parentId
UNION ALL
SELECT
t1.ExceptionRepositoryId, t1.ParentExceptionRepositoryId
FROM
dbo.tbl_ExceptionRepository t1
inner join TEMP t2 ON t1.ParentExceptionRepositoryId = t2.ExceptionRepositoryId
)
SELECT * FROM TEMP
No comments:
Post a Comment