Sqlserver
 sql >> Datenbank >  >> RDS >> Sqlserver

Rekursive SQL-Abfrage

Tauschen Sie einfach die Felder in der Join-Klausel aus:

WITH CategoryTree AS
        (
        SELECT  *, 0 AS Generation    
        FROM    dbo.Category
        WHERE   CategoryName = 'Animation'
        UNION ALL
        SELECT  Cat.*, Generation + 1    
        FROM    CategoryTree
        JOIN    dbo.Category AS Cat
        ON      Cat.Id = CategoryTree.CategoryMaster
        )
SELECT  *
FROM    CategoryTree