Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Building a string in SQL

Author  Topic 

EsQueueEll
Starting Member

2 Posts

Posted - 2010-09-30 : 16:16:58
I'm not sure if this is possible with pure SQL but here's what I need to do.

Find all applicable users that need to be updated.

For each user, go to another table that holds information for them. If a column has one of the applicable values, start building a string. Set the string to another column value of another table through a foreign key. For each row that has an applicable value, add on to that string.

So an example would be:
Users
PK Name
1 Bob
2 John

User Information
PK VALUE Users FK
1 NULL 1
2 NULL 2

Information Values
PK VALUE INFO FK USER PK
1 55 lbs 1 1
2 French 2 1


Information Categories
PK NAME
1 Bench Press
2 Spoken Language


So.. I would need the string "Bench Press, Spoken Language" for the first user, and so on for each other user, if it exists in the information values database, then put it in the user information database.

I've been able to select the appropriate rows for each user, it's where I need to build the string I get completely lost. In setting my string after select, it only sets it to the last value of the select statement. So I definitely don't understand SQL at all in this capacity, nested loops don't even seem to work for me.

Thanks for any help pointing me in the right direction!

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2010-09-30 : 17:18:11
check out this topic for various ways to build a CSV (comma separated value). The topic has links with other solutions as well.
Concatenating data from rows with same GroupBy column

Be One with the Optimizer
TG
Go to Top of Page
   

- Advertisement -