代码之家  ›  专栏  ›  技术社区  ›  rs.

从数据库生成面包屑

  •  1
  • rs.  · 技术社区  · 15 年前

     id       parent    order   tab       desc
    ------------------------------------------------------------------------
        1        Null   1   False       abcdef
        2       Null    2   False       efgh
        3       1       1   False       sadad
        4       1       2   False       aasd
        5       3       1   True        qwer
        6       3       1   True        asdad
        7       5       1   False       zxzc
        8       5       2   False       okli
    

    我想用这些数据生成xml和breadcrumb,如何用这些数据实现这一点?

    1 回复  |  直到 15 年前
        1
  •  2
  •   Jeremy Elbourn    15 年前

    对于breadcrumb,您需要使用如下递归CTE:

    ;with Tree as
    (
       select CONVERT(varchar(100), id) as Path, id
       from Tbl
       where Tbl.Parent is null
    
       union all
    
       select Tree.Path + ' > ' + id as Path, id
       from Tbl
    
            inner join 
            Tree
            on Tree.id = Tbl.Parent
    )
    
    select * from Tree