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 1019845
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T11:04:33+00:00 2026-05-16T11:04:33+00:00

Using SQL Server 2008 and any available features of TSQL, I am trying to

  • 0

Using SQL Server 2008 and any available features of TSQL, I am trying to work out how to if a set-based solution that does not involve a temp table exists for the following problem:

Given a set of nodes that have a parent-child relationship, and a set of key-value pairs that apply to each, and given that
the value (for a given key-value pair) at a deeper level of the node hierarchy will override a value with the same key that
is inherited from an ancestor node, select:

  1. the full set of key-value pairs that apply to a given node
  2. the set of inherited values for that node

The schema is as follows:

create table Node
(
    ID bigint identity primary key,
    ParentID bigint null foreign key references Node(ID),
    Name nvarchar(100)
);

create table KeyValuePair
(
    ID bigint identity primary key,
    KeyName nvarchar(100) not null,
    Value nvarchar(1000) not null,
    NodeID bigint not null foreign key references Node(ID),

    unique (KeyName, NodeID)
);

The result set would essentially include the columns KeyName, Value, InheritedValue.

I’ve been trying to do this using a common table expression but the logic of it is a bit tricky.

  • 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-16T11:04:34+00:00Added an answer on May 16, 2026 at 11:04 am

    I setup the Node and KeyValuePair tables as per the question, and populated with some sample values, such that my hierarchy was as follows:

    Root
    |—A
    |   |—A1
    |   |—A2
    |
    |—B
        |—B1
        |—B2

    I assigned two properties, named “Property 1” and “Property 2”, each defined in root with values “Root Prop 1” and “Root Prop 2” respectively. In A, I overrode “Property 1” with value “A Prop 1” and in B, I overrode “Property 2” with value “B Prop 2”.

    set identity_insert Node on
    insert into Node(ID,ParentID,Name)
    values (1,null,'Root'),(2,1,'A'),(3,1,'B'),(4,2,'A1'),(5,2,'A2'),
           (6,3,'B1'),(7,3,'B2')
    set identity_insert Node off
    
    insert into KeyValuePair(KeyName, [Value], NodeID)
    values ('Property 1','Root Prop 1',1),
    ('Property 2','Root Prop 2',1),
    ('Property 1','A Prop 1',2),
    ('Property 2','B Prop 2',3)
    

    Calling Nathan’s solution for node A1 yields no rows!

    The where clause in Nathan’s solution should be a condition of the join between keys and v, resulting in the revised procedure shown below (also I’ve renamed DataValue to KeyValuePair to be consistent with the original question):

    create procedure dbo.ListDataValues
        @nodeid bigint
    as
    begin
        with nodes as (
            select ID, ParentID, 0 as Level
            from Node n where ID=@nodeid
            union all
            select n.ID, n.ParentID, c.Level+1 as Level
            from Node n inner join nodes c on c.ParentID = n.ID
        ),
        keys as (
            select distinct(KeyName)
            from KeyValuePair
            where NodeID in (select ID from nodes)
        )
        select
            keys.KeyName,
            v.Value,
            i.Value as [InheritedValue],
            i.NodeID as [InheritedFromNodeID]
        from
            keys
            left join KeyValuePair v on v.KeyName = keys.KeyName
                                         and v.NodeID = @nodeid
            left join KeyValuePair i on i.KeyName = keys.KeyName
                and i.NodeID = (select top 1 NodeID from KeyValuePair d
                                inner join nodes k on k.ID = d.NodeID
                                where Level > 0 and d.KeyName = i.KeyName
                                order by [Level])
    end
    go
    

    This yielded the correct results as expected:

    KeyName      Value   InheritedValue    InheritedFromNodeID
    ------------ ------- ----------------- --------------------
    Property 1   NULL    A Prop 1          2
    Property 2   NULL    Root Prop 2       1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

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.