SQL concatenate column in one-to-many

Hi,

I need to populate a column in a table with multiple values from another table.  The values need to be comma seperated.  I think I could do this with a cursor, but I'm trying to find a different way.  What else can I try?

I've tried this:
update #component_string
       set a.cmpt_agin = Stuff((Select ',' + b.cmpt_agin From component b where a.part_key = b.part_key For XML Path('')), 1, 1, '')
       from #component_string a

And I've tried this:
/*update #component_string
       set a.cmpt_agin = isnull(a.cmpt_agin + ', ', '') + b.cmpt_agin_nbr
       from #component_string a,
            component b
       where a.part_key = b.part_key*/
coperations07Asked:
Who is Participating?
 
coperations07Author Commented:
I ended up just using a cursor to accomplish what I needed.  It doesn't seem to be affecting performance any.
0
 
cyberkiwiCommented:
You update "a" the alias instead

update a
       set a.cmpt_agin = Stuff((Select ',' + b.cmpt_agin From component b where a.part_key = b.part_key For XML Path('')), 1, 1, '')
       from #component_string a
0
 
coperations07Author Commented:
I'm getting an error that says: incorrect syntax near 'Path'

I'm querying a Sybase database btw.
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

 
coperations07Author Commented:
Is there anything else I can try besides a cursor?
0
 
cyberkiwiCommented:
Sorry the syntax you showed was for SQL Server (or looks like), so I thought I could help. I don't have Sybase to help you.
0
 
coperations07Author Commented:
hmmm..k   It probably is for SQL Server, but I did see the stuff function on the sybase site as well.  I shall keep tinkering with it. thanks.
0
 
coperations07Author Commented:
The cursort worked fine. Never could get the Stuff function to work.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.