[英]SQL Recursive concatenation of a file and path name
I just can't wrap my head around this... I have 3 related tables in SQL Server: 我只是无法解决这个问题... SQL Server中有3个相关表:
FileIds FileIds
Id
FileName
FolderIds 资料夹编号
Id
ParentId
FolderName
FileInfo 文件信息
Id
FolderId
FileId
FileAuthorId
FileClientId
Sample data: 样本数据:
FileIds FileIds
1, Gartner
2, Parker
3, Stepp
FolderIds 资料夹编号
1, null, Georgia
2, 1, Atlanta
3, 2, Terminus
4, 3, Suite 1500
5, 4, David Williams
6, 4, Lisa Davidson
7, 2, LaGrange
8, 7, Dena Pressley
FileInfo 文件信息
1, 8, 1, null, null
2, 5, 2, null, null
3, 6, 3, null null
Sample output from view: 来自视图的样本输出:
Georgia.Atlanta.LaGrange.Dena Pressley:Gartner, null, null
Georgia.Atlanta.Terminus.Suite 1500.David Williams:Parker, null, null
Georgia.Atlanta.Terminus.Suite 1500.Lisa Davidson:Stepp, null, null
While this is not for a file directory, it is laid out like one (replace .'s and :'s with \\'s) and I need to create a view that shows the full path... It is easy to put the pieces together in code such as C#, but I can't seem to figure out how to do it in SQL. 虽然这不是文件目录,但它的布局像一个(用\\代替。和and的),我需要创建一个显示完整路径的视图...放置它很容易在C#等代码中组合在一起,但是我似乎无法弄清楚如何在SQL中实现。 I have looked up SQL Server's recursive CTE and I'm just not getting it.
我查找了SQL Server的递归CTE,但我没有。
This should allow you to start using recursive CTE: 这应该允许您开始使用递归CTE:
In here I'm only adding the folders, but you can do exactly the same, in another CTE to add the files 在这里,我仅添加文件夹,但是您可以完全相同,在另一个CTE中添加文件
WITH folders (Id, ParentId, FolderName)
AS (
-- In here you select the first LEVEL
SELECT Id, ParentId, FolderName FROM FolderIds
WHERE ParentId IS NULL
UNION ALL
-- In here you join the previous LEVEL with new relative LEVELS
SELECT b.Id, b.ParentId, a.FolderName CONCAT ' ' CONCAT b.FolderName
FROM folders a
INNER JOIN FolderIds b
ON a.Id = b.ParentId
)
SELECT *
FROM folders
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.