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

  • SEARCH
  • Home
  • 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 842219
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T05:55:27+00:00 2026-05-15T05:55:27+00:00

I have a SqlServer2005 table Group similar to the following: Id (PK, int) Name

  • 0

I have a SqlServer2005 table “Group” similar to the following:

Id (PK, int)
Name (varchar(50))
ParentId (int)

where ParentId refers to another Id in the Group table. This is used to model hierarchical groups such as:

Group 1 (id = 1, parentid = null)
    +--Group 2 (id = 2, parentid = 1)
    +--Group 3 (id = 3, parentid = 1)
           +--Group 4 (id = 4, parentid = 3)
Group 5 (id = 5, parentid = null)
    +--Group 6 (id = 6, parentid = 5)

You get the picture

I have another table, let’s call it “Data” for the sake of simplification, which looks something like:

Id (PK, int)
Key (varchar)
Value (varchar)
GroupId (FK, int)

Now, I am trying to write a stored proc which can get me the “Data” for a given group. For example, if I query for group 1, it returns me the Key-Value-Pairs from Data where groupId = 1. If I query for group 2, it returns the KVPs for groupId = 1, then unioned with those which have groupId = 2 (and duplicated keys are replaced).

Ideally, the sproc would also fail gracefully if there is a cycle (ie if group 1’s parent is group 2 and group 2’s parent is group 1)

Has anyone had experience in writing such a sproc, or know how this might be accomplished?

Thanks guys, much appreciated,

Alex

  • 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-15T05:55:27+00:00Added an answer on May 15, 2026 at 5:55 am

    This can be accomplished using recursive queries and CTEs

    ;WITH KeyValue AS
    (
    SELECT G.Id, G.Name, D.Key, D.Value, 0 AS level
    FROM Group G
    LEFT JOIN Data D ON G.Id = D.GroupID
    WHERE G.Id = @your_top_level_root
    
    UNION ALL
    
    SELECT G.Id, G.Name, D.Key, D.Value, K.level + 1
    FROM KeyValue K
    JOIN Group G ON G.ParentId = K.Id
    LEFT JOIN Data D ON G.Id = D.GroupID
    )
    SELECT * FROM KeyValue
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this ASP.NET/SQLServer2005 website running on a production server (Win2003, QuadCore, 4GB). The
Lets say we have a following table with two columns and following rows of
I have a table (on sqlserver05) with an xml column. The format of the
Have you used VS.NET Architect Edition's Application and System diagrams to start designing a
I'm using SQLServer2008 Express I have a table and a script to convert the
I have got a table which contains 5 column and query requirements: update row
I have done the following tutorial: http://www.anddev.org/networking-database-problems-f29/connecting-to-mysql-database-t50063.html for connecting an android device to an
I have a table like Id Value 1 Start 2 Normal 3 End 4
I have database in which data is imported from the other table. if data
I have following query select * from table1 if table1 contains text type column/columns

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.