Page_ID Page_Name Page_Title Page_Handler Parent_Page_ID Language_ID Region_ID
1 Home Home index.aspx 0 1 uae
2 Personal Personal index.aspx 0 1 uae
3 Accounts & Deposits Accounts & Deposits index.aspx 2 1 uae
4 Current Account Current Account current-account.aspx 3 1 uae
5 Current Gold Accounts gold Account gold-account.aspx 3 1 uae
6 Easy Saver Easy Saver Account saver-account.aspx 3 1 uae
7 Fixed Deposits Fixed Account fixed-account.aspx 3 1 uae
8 Loans Loans index.aspx 2 1 uae
9 Personal Loans Personal Loans index.aspx 8 1 uae
10 car Loans car Loans car-loan.aspx 8 1 uae
我希望生成以下路径,用于ASP.NET路由,我可以在面包屑导航中使用相同的路径。
www.abc.com/home
www.abc.com/personal
www.abc.com/personal/acounts-deposits/
www.abc.com/personal/acounts-deposits/current-account
www.abc.com/personal/acounts-deposits/current-gold-account
www.abc.com/personal/acounts-deposits/easy-saver
......
......
www.abc.com/personal/loans/
www.abc.com/personal/loans/personal-loans
www.abc.com/personal/loans/car-loans
我修改了一个CTE脚本,使其与我的表结构类似。但是这个CTE的问题在于它给出了我需要的兄弟节点/子节点/父节点
,而不是父节点/子节点/兄弟节点
的路径。
示例 http://sqlfiddle.com/#!3/0d086/1
样例代码-由于sqlfiddle的限制,我使用了短数据。
CREATE TABLE PageMenu
([PageId] int, [PageName] varchar(5), [PageInheritance] int)
;
INSERT INTO PageMenu
([PageId], [PageName], [PageInheritance])
VALUES
(1, 'home', 0),
(2, 'p1', 0),
(3, 'c1', 2),
(4, 'c2', 2),
(5, 's3', 4),
(6, 'S3a', 5)
;
WITH CategoryCTE AS
( SELECT PageId,
PageName,
PageInheritance,
RecursionLevel = 1,
ParentRoot = CAST('None' AS VARCHAR(MAX)),
LastParentCatID = PageInheritance
FROM PageMenu
UNION ALL
SELECT cte.PageId,
cte.PageName,
cte.PageInheritance,
cte.RecursionLevel + 1,
ParentRoot = CASE WHEN cte.RecursionLevel = 1 THEN '' ELSE cte.ParentRoot + '/' END + c.PageName,
LastParentCatID = c.PageInheritance
FROM CategoryCTE cte
INNER JOIN PageMenu c
ON c.PageId = cte.LastParentCatID
), MaxRecursion AS
( SELECT PageId,
PageName,
PageInheritance,
ParentRoot,
RowNum = ROW_NUMBER() OVER(PARTITION BY PageId ORDER BY RecursionLevel DESC)
FROM CategoryCTE
)
SELECT PageId, PageName, PageInheritance, ParentRoot
FROM MaxRecursion
WHERE RowNum = 1 ORDER BY PageId DESC;