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.
| Author |
Topic |
|
ackweb
Yak Posting Veteran
54 Posts |
Posted - 2005-04-23 : 19:54:29
|
| I'm using the following query to generate a CSV list of the donors for a particular item in my database:DECLARE @ExampleCSV varchar(400)DECLARE @ItemID intSET @ItemID = 25SELECT @ExampleCSV = (CASE ISNULL(@ExampleCSV,'') WHEN '' THEN DonorName ELSE @ExampleCSV + ', ' + DonorName END) FROM tblDonorList WHERE ItemID = @ItemIDSELECT @ItemID AS ItemID, @ExampleCSV AS DonorsThe example results for this query are as follows:ItemID ---- Donors25 -------- Bob Smith, Joe GreenIs there a way to generate a similar csv list of donors by item for all the items?ItemID ---- Donors25 -------- Bob Smith, Joe Green26 -------- Peggy Brown, Will TrumpThe only thing that I've come up with is to use a User Defined Function and return the CSV donor list for each item incrementally. However, this is painfully slow when there is a long list of items. I'd appreciate any guidance on this. |
|
|
Kristen
Test
22859 Posts |
Posted - 2005-04-24 : 02:58:18
|
| Can't think of a smart way off hand, but your example could be a little slicker (although i don't know if it would make it faster)SELECT @ExampleCSV = NULL -- Not required if @ExampleCSV has been DECLARED but not usedSELECT @ExampleCSV = COALESCE(@ExampleCSV + ', ', '') + DonorNameFROM tblDonorList WHERE ItemID = @ItemIDKristen |
 |
|
|
mohdowais
Sheikh of Yak Knowledge
1456 Posts |
Posted - 2005-04-24 : 06:22:16
|
This is by far the most amazing solution I have seen for your question: http://www.sqlteam.com/item.asp?ItemID=11021Nobody really knows why this works, but who cares, as long as it works! The only other solution i can think of is using a UDF, but as you have seen from your own experience, they can be quite slow with larger sets.OS |
 |
|
|
|
|
|
|
|