Home Forums SQL Server 2008 T-SQL (SS2K8) Need Help Deconstructing Tree Hierarchy Adjacency menu Table with T-SQL RE: Need Help Deconstructing Tree Hierarchy Adjacency menu Table with T-SQL

  • Grasshopper: I'd a couple typos on my part on field names... i changed your query (below). When i run it now, i get no errors, but I also get zero records. You are doing some things I am not familiar with... What does the "with cte as" statement do - is it defining a dataset? If so, how can you have "cte" within your with-clause?

    with cte as

    (select MenuID,MenuDesc,ParentMenuID,Sequence, 1 as Lev

    from Menu

    where ParentMenuID is null

    UNION ALL

    select m.MenuID,m.MenuDesc,m.ParentMenuID,m.Sequence,Lev+1

    from cte

    join Menu m

    on m.ParentMenuID=cte.MenuID

    )select MenuID,MenuDesc,ParentMenuID,Sequence,ROW_NUMBER()over(order by MenuID) as [Order],Lev

    from cte

    The order column i want to add will simply put the menu in the correct order when fully expanded. Note that my second example above places the original data into the correct order and correctly identifies the indention-level as well. It should start with 1, and end with 1682.

    My objective is to create this query to make it easy to get the menu into an excel spreadsheet. The Menu is from an ERP system, and i need to have users go through and make some decisions regarding security. The easiest way for the users to conceptualize this is in Excel, but i cannot take this Adjacent listing of the menu table and put it into a recognizable form without doing it manually.

    Thanks,

    Keith