sql >> Databasteknik >  >> RDS >> Sqlserver

SQL Server Tree Query

WITH  OrganizationsH (OrgParentFK, OrgPK, OrgName, level, Label) AS
(
    SELECT OrgParentFK, OrgPK, OrgName, 0, CAST(OrgName AS VARCHAR(MAX)) As Label
    FROM Organizations
    WHERE OrgParentFK IS NULL
    UNION ALL
    SELECT o.OrgParentFK, o.OrgPK, o.OrgName, level + 1,  CAST(h.Label + '/' + o.OrgName  VARCHAR(MAX)) As Label
    FROM Organizations o JOIN OrganizationsH h ON o.OrgParentFK = h.OrgPK
)

SELECT OrgParentFK, OrgPK, OrgName, level, Label
FROM OrganizationsH
WHERE OrgPK = 5

h/t till marc_s



  1. rad till kolumn konvertering i mysql

  2. Hämta data och skriva ut det i en HTML-tabell

  3. Hur får man automatiska meddelanden om ändringar i tabeller?

  4. Hur man ökar intervalldatumet på Mysql