Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
T-SQL中树(菜单)结构的表示_Sql_Sql Server_Tsql_Relational Database - Fatal编程技术网

T-SQL中树(菜单)结构的表示

T-SQL中树(菜单)结构的表示,sql,sql-server,tsql,relational-database,Sql,Sql Server,Tsql,Relational Database,我有这张桌子 id parent ---------------- 1 0 2 0 3 1 4 3 5 2 我想将其导出到一个列中,并导出到一个新表中,如下所示。它将表示树(菜单)结构。因此,每个ID都有自己的行,但如果它连接到父ID,则会添加四个空格,以使层次结构清晰可见 ID etc... ------------- ------------ |1

我有这张桌子

id        parent
----------------
1         0 
2         0 
3         1 
4         3 
5         2
我想将其导出到一个列中,并导出到一个新表中,如下所示。它将表示树(菜单)结构。因此,每个ID都有自己的行,但如果它连接到父ID,则会添加四个空格,以使层次结构清晰可见

     ID           etc...
-------------  ------------
|1          |
|    3      |
|         4 |
|2          |   
|    5      |

我只能想出一个解决方案,使用一组游标,所以这取决于子对象的数量。我试图进行递归,但想不出一个解决方案,可以从一个表中获取数据,使用空格进行递归,将数据添加并导出到新表中的列中。谢谢

您可以使用递归CTE:

with cte as (
      select id, cast(id as varchar(max)) as hierarchy
      from t
      where parent = 0
      union all
      select t.id, cte.hierarchy + '-->' + cast(t.id as varchar(max))
      from cte join
           t
           on t.parent = cte.id
     )
select *
from cte;

这为层次结构提供了更明确的格式。虽然可以使用缩进,但我认为显式层次结构的信息量要大得多。

可以使用递归CTE:

with cte as (
      select id, cast(id as varchar(max)) as hierarchy
      from t
      where parent = 0
      union all
      select t.id, cte.hierarchy + '-->' + cast(t.id as varchar(max))
      from cte join
           t
           on t.parent = cte.id
     )
select *
from cte;
这为层次结构提供了更明确的格式。虽然可以使用缩进,但我认为显式层次结构的信息量要大得多。

这有帮助吗

    declare @tmp table (id int, parent int)
    declare @NewTable table (id VARCHAR(50))

    insert into @tmp
    SELECT 1,0
    union
    SELECT 2,0
    union
    SELECT 3,1
    union
    SELECT 4,3
    union
    SELECT 5,2



    ;with name_tree as (
       select id,id as p1,parent
       from @tmp
       --where id = 4 -- this is the starting point you want in your recursion
       union all
       select p.id,c.id as p1, c.parent
       from @tmp c
         join name_tree p on p.parent = c.id  -- this is the recursion
    ) 



    INSERT INTO @NewTable(id)
    select REPLICATE( ' ' , (Count(*)-1)*4 )+CONVERT(VARCHAR(10),id)
    from name_tree
    group by id

    select * from @NewTable
这有用吗

    declare @tmp table (id int, parent int)
    declare @NewTable table (id VARCHAR(50))

    insert into @tmp
    SELECT 1,0
    union
    SELECT 2,0
    union
    SELECT 3,1
    union
    SELECT 4,3
    union
    SELECT 5,2



    ;with name_tree as (
       select id,id as p1,parent
       from @tmp
       --where id = 4 -- this is the starting point you want in your recursion
       union all
       select p.id,c.id as p1, c.parent
       from @tmp c
         join name_tree p on p.parent = c.id  -- this is the recursion
    ) 



    INSERT INTO @NewTable(id)
    select REPLICATE( ' ' , (Count(*)-1)*4 )+CONVERT(VARCHAR(10),id)
    from name_tree
    group by id

    select * from @NewTable

查找
cte
,这更多的是格式化搜索递归cte。这已经做了成千上万次了。但有时不知道要搜索的术语超过了战斗的一半。查找
cte
,这更多的是格式化搜索递归cte。这已经做了成千上万次了。但有时不知道该搜索哪个词是大势所趋。谢谢。我现在只需要考虑一下格式是否正确。谢谢。我现在只需要考虑如何正确格式化它。