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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T12:24:55+00:00 2026-06-12T12:24:55+00:00

I have a table, which links to another table specifying the the value of

  • 0

I have a table, which links to another table specifying the the value of a key-value pair. The Value table then links to a table holding the name (key). The tables are defined (near enough) as follows:

---------------
Entity
---------------
EntityID
Name
Some other data

---------------
Value
----------------
ValueID
EntityID
Value

---------------
Key
---------------
KeyID
ValueID
Name

Each Entity can have many Values and each Value has one Key. Each Entity may not have the same number of Values. Basically, this structure is allowing me to add table rows dynamically, without modifying the Entity table.

I want to do a select * on the Entity table, using the Key.Name field as a column header, and displaying the appropriate Value.Values

Is there a dynamic structure available in SQL that I can do an insert into for each row in Entity, then return a select on?

I hope this makes sense!

  • 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-06-12T12:24:56+00:00Added an answer on June 12, 2026 at 12:24 pm

    Based on your description, it sounds like you want to PIVOT the data. There are two ways to do this with PIVOT, either a Static Pivot where you code the columns to transform or a Dynamic Pivot which determines the columns at execution.

    Static Pivot:

    select *
    from
    (
      select e.entityid, v.value,
        e.name EntityName, k.name KeyName
      from value v
      left join entity e
        on v.entityid = e.entityid
      left join [key] k
        on v.valueid = k.valueid
    ) x
    pivot
    (
      max(value)
      for KeyName in ([name 1], [name 2], [name 3], [name 4], [name 5])
    ) p;
    

    See SQL Fiddle With Demo

    A Dynamic Pivot determined the columns at run-time:

    DECLARE @cols AS NVARCHAR(MAX),
        @query  AS NVARCHAR(MAX)
    
    select @cols = STUFF((SELECT distinct ',' + QUOTENAME(name) 
                        from [key]
                FOR XML PATH(''), TYPE
                ).value('.', 'NVARCHAR(MAX)') 
            ,1,1,'')
    
    set @query = 'SELECT entityid, EntityName, ' + @cols + ' from 
                 (
                    select e.entityid, v.value,
                      e.name EntityName, k.name KeyName
                    from value v
                    left join entity e
                      on v.entityid = e.entityid
                    left join [key] k
                      on v.valueid = k.valueid
                ) x
                pivot 
                (
                    max(value)
                    for KeyName in (' + @cols + ')
                ) p '
    
    execute(@query)
    

    See SQL Fiddle With Demo

    Both of these can be placed in a stored procedure. If you post some sample data for each of your tables and then the desired result, it would be easier to determine the proper query.

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

Sidebar

Related Questions

I have a table which links the userID and the friendID (which is another
I have a table which links to another table in the ASP.NET membership schema.
I have a table which needs to link one of three seperate tables, but
I have a database that which contains two tables, links and tour. Tour is
I have one table which contains events and dates, and another which contains the
I have three tables: Context, Component and ComponentContext. The ComponentContext table links the Component
I have a database table which link locations together; a location can be in
I have table which contains double values stored in mysql database ...I need to
I have table in which I am inserting rows for employee but next time
i have a table which has many fields but i want to get count

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.