问题描述
我具有以下T-SQL功能: https://gist.github.com/cwattengard/11365802
I have the following T-SQL function: https://gist.github.com/cwattengard/11365802
这将以广度优先遍历返回数据.有没有简单的方法可以使此函数以深度优先遍历的方式返回其数据?我有一个Treeview组件可以满足这个要求(旧版系统).
This returns data in a breadth-first traversal. Is there a simple way to make this function return its data in a depth-first traversal? I have a treeview-component that excpects this (legacy system).
我已经有一个类似的存储过程,该存储过程以深度优先遍历的方式返回树,但它使用的是游标,而且速度很慢.(此功能需要6到7秒,而在同一数据上花费的时间不到一秒钟).
I already have a similar stored procedure that returns the tree in a depth-first traversal, but it's using cursors and is really slow. (6-7 seconds as opposed to this function that takes less than a second on the same data).
推荐答案
http://sqlanywhere.blogspot.in/2012/10/example-recursive-union-tree-traversal.html
下面的图显示了树形表的主键:
Here's a diagram showing the primary keys for a tree-structured table:
1
|
---------------------------------------
2 93 4 5
| | | |
-------------- ------------ -------- ------
6 7 8 9 10 11 12 13 14 15 16 17 18 19
| | | |
----- ----- ----- -----
27 26 25 24 23 22 21 20
以下是广度优先和深度优先查询应返回的内容:
Here's what the breadth-first and depth-first queries should return:
Breadth-First Depth-First
1 1
2 2
93 6
4 7
5 27
6 26
7 8
8 9
9 10
10 93
11 11
12 12
13 13
14 25
15 24
16 14
17 4
18 15
19 16
27 17
26 23
25 22
24 5
23 18
22 21
21 20
20 19
这篇关于在T-SQL中以广度优先的方式进行深度优先遍历的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!