I have written cte for tree view structured tableColumns: ID, ParentID, NameBelow query returns all data for passed parent upto n level;WITH OrderLinksCTEAS( SELECT Parentid = ParentMenuID, MenuID, MenuName, LVL = 1 FROM tbl_Menus WHERE ParentMenuID = 1 UNION ALL SELECT SML.ParentMenuID, SML.MenuID, SML.MenuName, LVL = c.LVL + 1 FROM OrderLinksCTE c INNER JOIN tbl_Menus SML ON SML.ParentMenuID = c.MenuID)SELECT *FROM OrderLinksCTE ORDER BY LVL
But I will pass one by one call to sql and will pass one parentid at a timeI need last column LEVEL -- If I pass 3rd level parent id then related LEVEL should come in return in list which returns at time of executing of all recordsCurrent outputID parentid name level2 1 a 1 3 1 b 1 4 3 c 2 5 3 d 2 6 3 e 2 Required outputIf i pass parent id 3 then level 2 should come in list.. currently above query returns 1 level
Please help