I have 2 tables, first contains data of documents and second for directories.
Table 1
DocID DirID Name Order
----- ----- ---- -----
1 4 Doc1 2
2 1 Doc2 1
3 5 Doc3 1
4 3 Doc4 1
5 4 Doc5 1
Table 2
DirID ParentID Name
----- -------- ----
1 NULL root
2 1 Dir1
3 2 Dir2
4 1 Dir3
5 3 Dir4
Structure
root
-Dir1
-Dir2
-Dir4
-Doc3
-Doc4
-Dir3
-Doc5
-Doc1
-Doc2
I'm trying to create CTE in T-SQL, which will generate this result, but I can't figure out how to do it. Can somebody suggest a solution?
Doc2
Dir3/Doc5
Dir3/Doc1
Dir1/Dir2/Doc4
Dir1/Dir2/Dir4/Doc3
Root is not shown, documents are sorted by Order within its directory and the result is sorted from the lowest depth ordered by name of full path.
ordercolumn name is a reserved word careful with syntax