Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7496049
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T18:27:25+00:00 2026-05-29T18:27:25+00:00

I am writing a stored procedure for retrieving the xml structure for a menu

  • 0

I am writing a stored procedure for retrieving the xml structure for a menu control. This seems to be a valid code( according to me & it is wrong) but does not run well on query tester. How could i correct it of the error below is the code

;WITH Hierarchy AS
(
    SELECT
        MenUid,MenuName,ApplicationId,ParentMenuId, 1 AS 'Level'
    FROM
        dbo.Menu
    WHERE
        ParentMenuId = '-1'

    UNION ALL

    SELECT
        M.MenUid,M.MenuName,M.ApplicationId,M.ParentMenuId, Level + 1 AS 'Level'
    FROM
        dbo.Menu M
    INNER JOIN
        Hierarchy h ON M.ParentMenuId = h.MenuId
)
SELECT *
FROM Hierarchy
ORDER BY [Level],[MenuName]

Where ParentMenuId and MenuId are related. Certain other doubts i got are below

  1. Is this perfectly fine to use CTE for menu. What other types have you been successful
  2. DataSet and ChildRelations are good too(or maybe i am wrong) for this purpose ?

Give me more ideas on how you have accomplished menu system in your projects.

Input

MenuId      MenuName      ApplicationId    ParentMenuId

 1          MenuName1           1              -1

 2          MenuName2           1               1

 3          MenuName3           1               -1

 4          MenuName4           1               2

Output

<Output>
    <Menu>
        <MenuId>1</MenuId>
        <MenuName>MenuName1</MenuName>
        <ApplicationId>1</ApplicationId>
        <ParentMenuId>-1</ParentMenuId>
        <SubMenu>
            <Menu>
                <MenuId>2</MenuId>
                <MenuName>MenuName2</MenuName>
                <ApplicationId>1</ApplicationId>
                <ParentMenuId>1</ParentMenuId>
                <SubMenu>
                    <MenuId>3</MenuId>
                    <MenuName>MenuName4</MenuName>
                    <ApplicationId>1</ApplicationId>
                    <ParentMenuId>3</ParentMenuId>
                </SubMenu>
            </Menu>
        </SubMenu>
    </Menu>
    <Menu>
        <MenuId>4</MenuId>
        <MenuName>MenuName3</MenuName>
        <ApplicationId>1</ApplicationId>
        <ParentMenuId>-1</ParentMenuId>
    </Menu>
</Output>

Don’t mind about the xml structure i is easy to get with FOR XML PATH i am not getting the query to run in the first place.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T18:27:27+00:00Added an answer on May 29, 2026 at 6:27 pm

    I’d wager that

    INNER JOIN
        Hierarchy h ON M.ParentMenuId = h.ParentMenuId
    

    should in fact be

    INNER JOIN
        Hierarchy h ON M.ParentMenuId = h.MenuId
    

    edit

    Using this sample data:

    declare @Menu table (
        MenuId int,
        MenuName nvarchar(20),
        ApplicationId int,
        ParentMenuId int
    )
    
    insert @Menu values
    (1, 'Name1', 1, -1),
    (2, 'Name2', 1,  1),
    (3, 'Name3', 1, -1),
    (4, 'Name4', 1,  2)
    

    and your query, with the above modification, I get

    MenUid      MenuName             ApplicationId ParentMenuId Level
    ----------- -------------------- ------------- ------------ -----------
    1           Name1                1             -1           1
    3           Name3                1             -1           1
    2           Name2                1             1            2
    4           Name4                1             2            3
    

    which looks correct. To help you we need to see all your relevant code.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a stored procedure. This procedure has a case where if it is
I am writing a stored procedure and within this procedure I am using isNULL.
I am writing a stored procedure in MySQL. The following is the code I
I am writing a stored procedure like this.. declare @avg float set @avg =
I'm writing a stored procedure to process XML data uploaded by the user: <People>
This is my first shot at writing a stored procedure. I'm trying to get
I'm writing one stored procedure, which I have to create a xml column from
I am writing a stored procedure where I have an input parameter called my_size
I'm writing a stored procedure that needs to have a lot of conditioning in
I am writing a stored procedure that when completed will be used to scan

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.