如何从完整路径创建父子层次结构

时间:2013-03-28 02:44:31

标签: sql-server-2008 hierarchy directory-structure

我在SQL Server 2008中存储了一个包含某些文档的完整路径的列。

我需要将路径转换为父子层次结构,如下所示。我需要这种结构,因为它需要我使用它的第三方软件。

这怎么做得最优雅?

自:

root/Dir1/Dir11/Dir3/Docname1.txt
root/Dir1/Dir12/Dir3/Docname2.txt
root/Dir1/Dir11/Docname3.txt
root/Dir1/Dir11/Dir31/Docname4.txt

要:

[Value]                 [Parent]
Docname1.txt            root/Dir1/Dir11/Dir3    
root/Dir1/Dir11/Dir3    root/Dir1/Dir11
root/Dir1/Dir11         root/Dir1
root/Dir1               root

Docname2.txt            root/Dir1/Dir12/Dir3
root/Dir1/Dir12/Dir3    root/Dir1/Dir12
root/Dir1/Dir12         root/Dir1
root/Dir1               root

Docname3.txt            root/Dir1/Dir11
root/Dir1/Dir11         root/Dir1
root/Dir1               root

Docname4.txt            root/Dir1/Dir11/Dir31
root/Dir1/Dir11/Dir31   root/Dir1/Dir11
root/Dir1/Dir11         root/Dir1
root/Dir1               root