我的数据库中有一个名为MyFolder的多层对象。 MyFolder可以是无限级别的另一个MyFolder的孩子。该表定义如下:
CREATE TABLE dbo.MyFolders
(
MyFolderId INT IDENTITY(1,1) NOT NULL,
ParentMyFolderId INT NULL,
Name NVARCHAR(50) NOT NULL,
Depth INT NOT NULL,
Ancestry NVARCHAR(max) NOT NULL,
CONSTRAINT PK_MyFolders PRIMARY KEY CLUSTERED (MyFolderId ASC),
CONSTRAINT FK_MyFolders_MyFolders FOREIGN KEY(ParentMyFolderId) REFERENCES dbo.MyFolders (MyFolderId)
)
它有如下数据:
MyFolderId ParentMyFolderId Name Depth Ancestry
1 NULL Folder1 0 /
2 1 Folder1A 1 /1/
3 1 Folder1B 1 /1/
4 1 Folder1C 1 /1/
5 4 Folder1C1 2 /1/4/
6 4 Folder1C2 2 /1/4/
7 6 Folder1C2a 3 /1/4/6/
8 6 Folder1C2b 3 /1/4/6/
这对我系统中所需的一切都很有效。但是,如果我想要检索如下的查询,那就很棘手了:
MyFolderId Name
1 Folder1
2 Folder1/Folder1A
3 Folder1/Folder1B
4 Folder1/Folder1C
5 Folder1/Folder1C/Folder1C1
6 Folder1/Folder1C/Folder1C2
7 Folder1/Folder1C/Folder1C2/Folder1C2a
8 Folder1/Folder1C/Folder1C2/Folder1C2b
有没有办法在祖先字段上加入以获取祖先的名字?或者使用ParentMyFolderId列的另一种方式?我有一个表值拆分字符串函数,名为SplitString(value,delimiter)。
答案 0 :(得分:1)
这可以使用递归查询来完成,只需将您当前的文件夹名称附加到之前的文件夹中即可。
<强>查询:强>
;WITH Source (MyFolderId, ParentMyFolderId, Name, Depth, Ancestry)
AS (
SELECT 1, NULL, 'Folder1', 0, '/'
UNION ALL
SELECT 2, 1, 'Folder1A', 1, '/1/'
UNION ALL
SELECT 3, 1, 'Folder1B', 1, '/1/'
UNION ALL
SELECT 4, 1, 'Folder1C', 1, '/1/'
UNION ALL
SELECT 5, 4, 'Folder1C1', 2, '/1/4/'
UNION ALL
SELECT 6, 4, 'Folder1C2', 2, '/1/4/'
UNION ALL
SELECT 7, 6, 'Folder1C2a', 3, '/1/4/6/'
UNION ALL
SELECT 8, 6, 'Folder1C2b', 3, '/1/4/6/'
),
cte AS
(
SELECT S.MyFolderID, S.ParentMyFolderId, CAST(S.Name AS VARCHAR(MAX)) AS Name
FROM Source AS S
WHERE ParentMyFolderId IS NULL
UNION ALL
SELECT S.MyFolderID, S.ParentMyFolderId, C.Name + '/' + S.Name
FROM Source AS S
INNER JOIN cte AS C
ON C.MyFolderId = S.ParentMyFolderId
)
SELECT *
FROM cte
答案 1 :(得分:0)
这里是评论中提到的递归CTE:
WITH TreeStructure(MyFolderId, Name) AS
(
SELECT MyFolderId, CONVERT(varchar(500), Name)
FROM MyFolders WHERE ParentMyFolderId IS NULL
UNION ALL
SELECT sd.MyFolderId, CONVERT(varchar(500), t.Name + '/' + sd.Name)
FROM MyFolders sd
JOIN TreeStructure t ON sd.ParentMyFolderId = t.MyFolderId
WHERE sd.ParentMyFolderId IS NOT NULL
)
SELECT * FROM TreeStructure
结果:
MyFolderId Name
----------- ----------------------------------------
1 Folder1
2 Folder1/Folder1A
3 Folder1/Folder1B
4 Folder1/Folder1C
5 Folder1/Folder1C/Folder1C1
6 Folder1/Folder1C/Folder1C2
7 Folder1/Folder1C/Folder1C2/Folder1C2a
8 Folder1/Folder1C/Folder1C2/Folder1C2b