Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
michaelgr
Starting Member
9 Posts |
Posted - 2012-04-18 : 09:01:32
|
Hello,I have a table - FolderID , FolderName, Parent_Folder_IDFolderID is the primary KeyParent_folder_ID is the foreign key for the FolderID primary key.in this table i can define "folders" with no deep limit. so folder can contain folders and each folder can contain more folders etc. I need to write a recursive query to get all folders and their child folders etc. How can i do it? |
|
sunitabeck
Master Smack Fu Yak Hacker
5155 Posts |
Posted - 2012-04-18 : 09:14:19
|
[code];WITH cte AS( SELECT *, CAST('\' AS VARCHAR(MAX)) AS Path FROM Tbl WHERE Parent_folder_Id IS NULL UNION ALL SELECT t.*,c.Path+c.FolderName+'\' FROM Tbl t INNER JOIN cte c ON c.FolderId = t.Parent_Folder_id)SELECT * FROM cte;[/code]Code parses, but logic is untested. |
 |
|
|
|
|