📜  递归查询分层数据sql server - SQL 代码示例

📅  最后修改于: 2022-03-11 15:05:29.594000             🧑  作者: Mango

代码示例1
WITH cte_org AS (
    SELECT       
        staff_id, 
        first_name,
        manager_id
        
    FROM       
        sales.staffs
    WHERE manager_id IS NULL
    UNION ALL
    SELECT 
        e.staff_id, 
        e.first_name,
        e.manager_id
    FROM 
        sales.staffs e
        INNER JOIN cte_org o 
            ON o.staff_id = e.manager_id
)
SELECT * FROM cte_org;
Code language: SQL (Structured Query Language) (sql)