I’m trying to list all files in a virtual directory and it’s sub directories. This could be employees in a company with subordinates, this is not a file system. Maybe recursive store procedure may not be the answer.
Scenario:
- Directory Table:
DirId, ParentId - Files Table:
FileId, DirId
ParentId is the parent directory and root directory has parentId = NULL… think it’s self explanatory.
Now the problem… I want a list files that exist in a directory and its sub directories.
For just one directory I would create a stored procedure:
SELECT * FROM Files Where DirId = ????
So how would I create a stored procedure to include sub directories? At the moment I am using C# code and looping through each directory. I prefer to use a stored procedure… unless you prove me wrong.
Have a look at using a CTE.
Something like