Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Dynamic Query/sproc Expand / Collapse
Author
Message
Posted Wednesday, May 5, 2010 10:39 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Tuesday, August 12, 2014 7:16 AM
Points: 150, Visits: 391
I need to create a stored procedure or query that will dynamically create columns from data in a column.

i.e.
mytable
field1
tom, gary, Thomas, bob
alex, bill, bob, dennis, gary

I want to run a query/sproc that will read the first row and create 4 columns and each column will have the name in it. The second row will then create the 5th column and insert all names in the corresponding columns.

i.e.
Start with this:
Field1
tom, gary, Thomas, bob
alex, bill, bob, dennis, gary

Run the query/sproc and then get this:
Field1 name1 name2 name3 name4 name5
tom, gary, Thomas, bob tom gary Thomas bob
alex, bill, bob, dennis, gary alex bill bob dennis gary




Post #916272
Posted Wednesday, May 5, 2010 2:33 PM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 12:23 AM
Points: 7,125, Visits: 13,171
Why do you want to do it dynamic?
Based on your sample data I would use a string split function to separate the names (the TallyTable article referenced in my signature has an example of such a function). Based on that I would either use a CrossTab (known max. number of names) or a DynamicCrossTab concept to create and populate the table you're looking for. Both articles are referenced in my signature as well.




Lutz
A pessimist is an optimist with experience.

How to get fast answers to your question
How to post performance related questions
Links for Tally Table , Cross Tabs and Dynamic Cross Tabs , Delimited Split Function
Post #916480
Posted Wednesday, May 5, 2010 4:04 PM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Tuesday, August 12, 2014 7:16 AM
Points: 150, Visits: 391
The Dynamic part is for the creation of the fields. It starts out with the 1 column with the string of names (comma delimited).


Post #916551
Posted Wednesday, May 5, 2010 4:17 PM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 12:23 AM
Points: 7,125, Visits: 13,171
alex abenitez (5/5/2010)
The Dynamic part is for the creation of the fields. It starts out with the 1 column with the string of names (comma delimited).

Do you know how many field you'll end up with? Will it always be five or less?




Lutz
A pessimist is an optimist with experience.

How to get fast answers to your question
How to post performance related questions
Links for Tally Table , Cross Tabs and Dynamic Cross Tabs , Delimited Split Function
Post #916558
Posted Wednesday, May 5, 2010 6:00 PM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Tuesday, August 12, 2014 7:16 AM
Points: 150, Visits: 391
I don't know how many fields it will be but I expect it to be less than 30.


Post #916595
Posted Saturday, May 8, 2010 4:46 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 12:23 AM
Points: 7,125, Visits: 13,171
alex abenitez (5/5/2010)
I don't know how many fields it will be but I expect it to be less than 30.


Did you have the time to read the articles I mentioned in my first reply? Seems to be that DynamicCrossTab is the way to go.
Or, alternatively, do it in the application layer.




Lutz
A pessimist is an optimist with experience.

How to get fast answers to your question
How to post performance related questions
Links for Tally Table , Cross Tabs and Dynamic Cross Tabs , Delimited Split Function
Post #918480
Posted Saturday, May 8, 2010 5:48 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Tuesday, August 12, 2014 7:16 AM
Points: 150, Visits: 391
Yes, working on it. I got side tracked and got pulled away on a critical project. I will follow up in a week. Thanks for your help.



Post #918485
Posted Saturday, May 8, 2010 7:58 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 9:09 AM
Points: 36,941, Visits: 31,443
alex abenitez (5/5/2010)
The Dynamic part is for the creation of the fields.


Heh... we know that, Alex. How will it be used? I ask because that's a large key to how to do this.

I'm glad you got it working but I'd still be interested in know the "why" and what you did to solve it. We might be able to make it a wee bit faster (or not).


--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #918596
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse