Log in
::
Register
::
Not logged in
Home
Tags
Articles
Editorials
Stairways
Forums
Scripts
Videos
Blogs
QotD
Books
Ask SSC
SQL Jobs
Training
Authors
About us
Contact us
Newsletters
Write for us
Recent Posts
Recent Posts
Popular Topics
Popular Topics
Home
Search
Members
Calendar
Who's On
Home
»
SQL Server 7,2000
»
T-SQL
»
Dynamic Query/sproc
Dynamic Query/sproc
Rate Topic
Display Mode
Topic Options
Author
Message
GrassHopper
GrassHopper
Posted Wednesday, May 05, 2010 10:39 AM
SSC Journeyman
Group: General Forum Members
Last Login: Monday, February 18, 2013 8:19 AM
Points: 87,
Visits: 211
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
LutzM
LutzM
Posted Wednesday, May 05, 2010 2:33 PM
SSCertifiable
Group: General Forum Members
Last Login: Wednesday, April 24, 2013 3:17 PM
Points: 6,731,
Visits: 12,131
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
GrassHopper
GrassHopper
Posted Wednesday, May 05, 2010 4:04 PM
SSC Journeyman
Group: General Forum Members
Last Login: Monday, February 18, 2013 8:19 AM
Points: 87,
Visits: 211
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
LutzM
LutzM
Posted Wednesday, May 05, 2010 4:17 PM
SSCertifiable
Group: General Forum Members
Last Login: Wednesday, April 24, 2013 3:17 PM
Points: 6,731,
Visits: 12,131
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
GrassHopper
GrassHopper
Posted Wednesday, May 05, 2010 6:00 PM
SSC Journeyman
Group: General Forum Members
Last Login: Monday, February 18, 2013 8:19 AM
Points: 87,
Visits: 211
I don't know how many fields it will be but I expect it to be less than 30.
Post #916595
LutzM
LutzM
Posted Saturday, May 08, 2010 4:46 AM
SSCertifiable
Group: General Forum Members
Last Login: Wednesday, April 24, 2013 3:17 PM
Points: 6,731,
Visits: 12,131
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
GrassHopper
GrassHopper
Posted Saturday, May 08, 2010 5:48 AM
SSC Journeyman
Group: General Forum Members
Last Login: Monday, February 18, 2013 8:19 AM
Points: 87,
Visits: 211
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
Jeff Moden
Jeff Moden
Posted Saturday, May 08, 2010 7:58 PM
SSC-Dedicated
Group: General Forum Members
Last Login: Today @ 8:14 AM
Points: 32,910,
Visits: 26,802
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 "
R
ow-
B
y-
A
gonizing-
R
ow".
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."
For better, quicker answers on T-SQL questions, click on the following...
http://www.sqlservercentral.com/articles/Best+Practices/61537/
For better answers on performance questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
Post #918596
« Prev Topic
|
Next Topic »
Permissions
You
cannot
post new topics.
You
cannot
post topic replies.
You
cannot
post new polls.
You
cannot
post replies to polls.
You
cannot
edit your own topics.
You
cannot
delete your own topics.
You
cannot
edit other topics.
You
cannot
delete other topics.
You
cannot
edit your own posts.
You
cannot
edit other posts.
You
cannot
delete your own posts.
You
cannot
delete other posts.
You
cannot
post events.
You
cannot
edit your own events.
You
cannot
edit other events.
You
cannot
delete your own events.
You
cannot
delete other events.
You
cannot
send private messages.
You
cannot
send emails.
You
may
read topics.
You
cannot
rate topics.
You
cannot
vote within polls.
You
cannot
upload attachments.
You
may
download attachments.
You
cannot
post HTML code.
You
cannot
edit HTML code.
You
cannot
post IFCode.
You
cannot
post JavaScript.
You
cannot
post EmotIcons.
You
cannot
post or upload images.
Copyright © 2002-2013 Simple Talk Publishing. All Rights Reserved.
Privacy Policy.
Terms of Use.
Report Abuse.