Sqlserver
 sql >> Teknologi Basis Data >  >> RDS >> Sqlserver

Kueri Pohon SQL Server

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 ke marc_s



  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. Hindari duplikat dalam kueri INSERT INTO SELECT di SQL Server

  2. Bagaimana cara memetakan BigDecimal di Hibernate sehingga saya mendapatkan kembali skala yang sama dengan yang saya masukkan?

  3. COT() Contoh di SQL Server

  4. 3 Cara Mengembalikan String Beberapa Spasi di SQL Server

  5. Lupa Kata Sandi SQL Server