1

I have the following table structure storing files and folder hierarchies.

FolderInfo table

Id (key)
Name
ParentFolderId (nullable)

and

File table

FileId(key)
FileName
FolderId

The FolderInfo table has a ParentFolderId which is a foreign key to its own id. The root folder has a null value in the ParentFolderId, all other records refer to their parent record through it. I want to generate a result which contains the selected files and their complete path walking up the hierarchies.

Result

File
Location

I need to achieve this using some smart T-SQL. I would welcome if someone can show a common table expression based approach to recursively walk the hierarchy up.

1 Answer 1

4

For getting a single file's path you will use a recursive CTE of the form: with folderPath as ( select Id, ParentFolderId, Name from FolderInfo where Id = @FileFolderId union all select p.Id, p.ParentFolderId, p.Name from folderPath p join FolderInfo fi on fi.Id = p.ParentFolderId ) select @FullFolderPath += '/' +Name from folderPath where @FileFolderId is the Id value for the file you want the path for, and @FullFolderPath is the varchar(8000) variable to aggregate the path.

This will provide you with a good start, but it might need some tweaking.

4
  • Thanks Kerneels for this quick help. Though your code does not fully answer my question, it gave me an insight to try further and come up with the conclusion. I realised a fundamental problem with my question through this. The result I want to achieve can not be doen through Common Table Expression. In fact, I want a bottom to top hierarchy walk but the CTE is only good at top to bottom or rather drill down reporting. My data is already in the form of drill down report and all I want to achieve is fuse all that data together. I will write a routine to achieve this now.
    – dhruvin
    Dec 8, 2014 at 4:50
  • Thanks for accepting my answer @dhruvin. Are you sure about your statements about CTE not able to do a bottom up traversal? It really does not feel like there would be much difference between a bottom up or top down traversal. In fact, suppose the result set is bottom up then one can simply aggregate the paths diferently, i.e. @FullPath = Name + '/' + @FullPath so prepending instead of appending. Also, what would happen if you tweak the CTE a bit, maybe find a way to make the recursion escape clause be ParentFolderId should be null so that it would climb up to where that is satisfied? Dec 8, 2014 at 6:28
  • Alternatively you could explore the effect of joining diferenctly on the CTE: fi.ParentFolderId = p.Id but I'm speculating. Recursive CTE is very powerful. Dec 8, 2014 at 6:31
  • Actually it is possible to query from the bottom up; you just have to swap the join columns. dba.stackexchange.com/a/229520/8343 Sep 13, 2023 at 17:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.