| Author |
Topic |
|
Roy Slade
Starting Member
7 Posts |
Posted - 2004-02-09 : 16:10:17
|
| I have a table with multiple entries for comments. Each entry is a unique row. I need to combine all of the comments into one row. For example:Name CommentFred Nice GuyFred Brings DonutsJoe Hard workerJoe Stays lateJoe Never leaves earlyWhat I want is:Name CommentFred Nice Guy Brings DonutsJoe Hard Worker Stays Late Never leaves earlyI've tried cursors, updates, and a long list of select statements with no luck. Thanks in advance for any and all help. |
|
|
Roy Slade
Starting Member
7 Posts |
Posted - 2004-02-09 : 16:24:31
|
| Sorry, the formatting didn't come out right.For example:Name | Comment--------------Fred | Nice GuyFred | Brings DonutsJoe | Hard workerJoe | Stays lateJoe | Never leaves earlyWhat I want is:Name | CommentFred | Nice Guy Brings DonutsJoe | Hard Worker Stays Late Never leaves early |
 |
|
|
X002548
Not Just a Number
15586 Posts |
Posted - 2004-02-09 : 16:42:36
|
Understand that the order of data in the database does NOT gaurentee the way the data will come out the same way every time...you need to ORDER BY something...USE NorthwindCREATE TABLE myTable99(Col1 int IDENTITY(1,1), Col2 varchar(10), Col3 varchar(25))GOINSERT INTO myTable99(Col2,Col3)SELECT 'Fred','Nice Guy' UNION ALLSELECT 'Fred','Brings Donuts' UNION ALLSELECT 'Joe', 'Hard worker' UNION ALLSELECT 'Joe', 'Stays late' UNION ALLSELECT 'Joe', 'Never leaves early'GOCREATE TABLE #myTable00(Col2 varchar(10), NewLine varchar(8000))DECLARE @Col2 varchar(10), @NewLine varchar(8000)SELECT @NewLine = ''DECLARE myCursor99 CURSORFORSELECT DISTINCT Col2 FROM myTable99OPEN myCursor99FETCH NEXT FROM myCursor99 INTO @Col2WHILE @@FETCH_STATUS = 0 BEGIN SELECT @NewLine = @NewLine + Col3 + ' ' FROM myTable99 WHERE Col2 = @Col2 Order by Col1 INSERT INTO #myTable00(Col2, NewLine) SELECT @Col2, @NewLine SELECT @NewLine = '' FETCH NEXT FROM myCursor99 INTO @Col2 ENDCLOSE myCursor99DEALLOCATE myCursor99SELECT * FROM #myTable00GODROP TABLE #myTable00DROP TABLE myTable99GO Brett8-) |
 |
|
|
graz
Chief SQLTeam Crack Dealer
4149 Posts |
Posted - 2004-02-09 : 17:04:12
|
| You might be able to take this process (http://www.sqlteam.com/item.asp?ItemID=11021) for converting multiple rows into a CSV and just get rid of the comma and space. As Brett said, you'll need to make sure you have some sort of line number for ordering by but that shouldn't be a big deal.===============================================Creating tomorrow's legacy systems today.One crisis at a time. |
 |
|
|
jsmith8858
Dr. Cross Join
7423 Posts |
Posted - 2004-02-09 : 17:09:55
|
| if you have sql 2000, check out the comments for a great UDF solution to this problem. It's quite efficient as well.- Jeff |
 |
|
|
Page47
Master Smack Fu Yak Hacker
2878 Posts |
Posted - 2004-02-10 : 07:20:42
|
I spent a whole afternoon on that stupid article and now every time it gets referenced there has to be a PS pointing to byrmol's "better" solution ... bummer.Jay White{0} |
 |
|
|
jsmith8858
Dr. Cross Join
7423 Posts |
Posted - 2004-02-10 : 09:06:17
|
| I'm sorry man, but that's my FAVORITE UDF of all time ! a true hall of famer ...your article is great, though ... i've used that UPDATE technique more than once since I learned it hear at SqlTeam !! (though it still frightens me !) And if you don't have sql2000, that's the way to go.- Jeff |
 |
|
|
X002548
Not Just a Number
15586 Posts |
Posted - 2004-02-10 : 09:49:28
|
| Wow...what a history lessonWhat a noob...only here 1yr 1moCursors falling by the wayside...Brett8-) |
 |
|
|
Roy Slade
Starting Member
7 Posts |
Posted - 2004-02-10 : 13:26:54
|
My thanks to all. Byrmol's techmique works great (after I spotted the typo ) and with an added update will do exactly what I want to do.By the way, the line:set @list = list = caseshould read:set @list = RecList = case |
 |
|
|
Roy Slade
Starting Member
7 Posts |
Posted - 2004-02-10 : 13:41:26
|
My thanks to all. Byrmol's techmique works great (after I spotted the typo ) and with an added update will do exactly what I want to do.By the way, the line:set @list = list = caseshould read:set @list = RecList = case |
 |
|
|
|