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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T00:32:35+00:00 2026-05-19T00:32:35+00:00

I have a somewhat more complicated scenario, but I think it should be possible.

  • 0

I have a somewhat more complicated scenario, but I think it should be possible.

I have a large SPROC whose result is a set of characteristics for a set of persons.

So the Table would look something like this:

Property |	 Client1 	 Client 2	 Client3
-----------------------------------------------------------
Sex      |	 M       	 F      	  M
Age      |	 67      	 56     	  67
Income   |	 Low     	 Mid    	  Low

It’s built using cursors, iterating over different datasets.

The problem I am facing is that there is a varying number of Clients and Properties, so an equally valid result over different input sets might be:

Property |	 Client1 	 Client 2
-------------------------------------------
Sex      |	 M       	 F   
Age      |	 67      	 56  
Weight   |	 122     	 122 

The different number of properties is easy, those are just extra rows.

My problem is that I need to declare a temporary table with a varying number of columns.

There could be 2 clients or 100. Every client in guaranteed to have every property ultimately listed.

What SQL structure would statisfy this and how can I declare it and insert things into it?

I can’t just flip the columns and rows either because there is a variable number of each.

  • 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-19T00:32:36+00:00Added an answer on May 19, 2026 at 12:32 am

    First, you should consider normalizing your design like so:

    Create Table ClientAttributes
    (
        ClientId ....
        , Sex Char(1)...
        , Age int...
        , Income...
    )
    

    Second, the SQL language in general is not geared for dynamic column generation. To achieve this, you have to build the SQL statement at runtime (a.k.a. dynamic SQL) as a string. It is best to do this in the middle-tier or a reporting engine rather than in T-SQL.

    Third, an infinitely flexible design where you have no idea as to the number or types of properties or instances isn’t a design at all. Each table represents a collection of entities with known attributes. They are not wads of arbitrary data. The attributes (columns) need to be known at design time or you risk a Cthulhu design where nothing but chaos reins.

    Will Hughes’ suggestion is to use an Entity-Attribute-Value design (a.k.a. EAV). It takes a tremendous amount of discipline to do an EAV correctly. It only works if it is treated solely as a wad of data. I.e., no developer can ever filter on any specific attribute (i.e. hard code a query looking for a specific attribute name), ever calculate on the values and you will never be able to ensure consistency amongst specific values. If you can maintain that discipline, then an arbitrary wad of data as a limited portion to a larger design can work. However, once you decide to hard code a query looking for a specific attribute you have gone down the dark path and forever will it dominate your destiny. As the database grows, the performance will degrade significantly. You will have no data integrity (e.g. you have two attributes called “Age” and “Client Age” with some values being integers and others being text). EAVs can be a nightmare to maintain. It is far better for maintenance, reporting, querying etc. to have a normalized design.

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

Sidebar

Related Questions

I have a somewhat complicated branching structure at work (at least for me). It
Let's say I have a somewhat large (several millions of items, or so) list
I have to write a very large test suite for a complex set of
I have somewhat interesting development situation. The client and deployment server are inside a
Does VS2008 have somewhat C++0x standard support? DUPLICATE Visual Studio support for new C
UPDATE: I have somewhat resolved the issue. Just in case if anyone runs in
I have a somewhat messily-formatted Objective-C code base. Is there a way to have
I have a somewhat simple Client/Server solution running over C# remoting (System.Runtime.Remoting). The MarshalByRef
I have a somewhat-long-running stored procedure being called from a PB application. I want
I have following somewhat complex sql query which has horrible performance, 'certainly' due to

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.