List menu hierarchy using SQL Server hierarchyid

*爱你&永不变心* 提交于 2019-12-24 05:13:14

问题


I try to build a menu using the datatype hierarchyid.

I have the root node and the current selected node. now I want to list of all elements that are related wetween root and selected node AND there siblings.

I get all related elements with following sql query

DECLARE @rootNode hierarchyid, @selectedNode hierarchyid

SELECT @rootNode = MenuNode FROM CMS_Menu WHERE MenuItemID = 3;

SELECT @selectedNode = MenuNode FROM CMS_Menu WHERE MenuItemID =15;

SELECT CMS_Menu.MenuNode  
FROM CMS_Menu
WHERE @selectedNode.IsDescendantOf(MenuNode) = 1 /*all related elements*/
AND MenuNode.GetLevel() >= @rootNode.GetLevel() /*nothing below root*/

Now I have to do something like MenuNode.GetAncestor(1) = result for each row in the query above.

Does anyone have an idea how to get this in a sql query?

Thanks : )


回答1:


Not entirely sure I understand the question but could you not do something like the following with the WHERE clause:

WHERE @selectedNode.IsDescendantOf(MenuNode.GetAncestor(1)) = 1

Tom



来源:https://stackoverflow.com/questions/6422953/list-menu-hierarchy-using-sql-server-hierarchyid

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!