zoukankan      html  css  js  c++  java
  • SQL Server 树查询

    WITH tree
    AS
    (
    SELECT ParentAssetID, AssetID,1 AS x2level,nodename,
    CAST(nodename AS NVARCHAR(max)) x2name,
    CAST(+AssetID AS NVARCHAR(max)) x2id
    FROM dbo.Assets
    WHERE ParentAssetID IS null
    UNION ALL
    SELECT c.ParentAssetID, c.AssetID, tree.x2level + 1,c.nodename, 
    CAST(REPLICATE('-', x2level * 4) + c.nodename AS NVARCHAR(max)) x2name,
    tree.x2id +':|:'+ CAST(c.AssetID AS NVARCHAR(max)) x2id
    FROM dbo.Assets c
    INNER JOIN tree
    ON c.ParentAssetID = tree.AssetID
    )
    SELECT x2name, AssetID, ParentAssetID FROM tree
    ORDER BY x2id;

    ---President
    ------Vice President
    ---------CEO
    ---------CTO
    ------------Group Project Manager
    ---------------Project Manager 1
    ------------------Team Leader 1
    ---------------------Software Engineer 1
    ---------------------Software Engineer 2
    ------------------Test Lead 1
    ---------------------Tester 1
    ---------------------Tester 2
    ---------------Project Manager 2
    ------------------Team Leader 2
    ---------------------Software Engineer 3
    ---------------------Software Engineer 4
    ------------------Test Lead 2
    ---------------------Tester 3
    ---------------------Tester 4
    ---------------------Tester 5
     
  • 相关阅读:
    WinForm控件之【Button】
    P4168 蒲公英 题解
    U91741 题解
    树链剖分 学习笔记
    GCD 及 EXGCD 复习笔记
    javascript中的对象拷贝
    关于Vue.js的v-for,key的顺序改变,影响过渡动画表现
    ajax无刷新上传和下载
    站点开启https和http2
    windows挂载EFI分区
  • 原文地址:https://www.cnblogs.com/mingjing/p/6041523.html
Copyright © 2011-2022 走看看