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 |
AskSQLTeam
Ask SQLTeam Question
0 Posts |
Posted - 2004-12-08 : 08:04:32
|
Sumanesh writes "Recently I read one article “Converting Multiple Rows into a CSV string”(http://www.sqlteam.com/item.asp?ItemID=256)I have found one easy and more efficient way to achieve the same thing. First I have a table called test with 2 columns (ID and Data) And some data inserted into it.CREATE FUNCTION [dbo].CombineData(@ID SMALLINT)RETURNS VARCHAR(2000)ASBEGIN DECLARE @Data VARCHAR(2000) SET @Data = '' SELECT @Data = @Data + Data + ',' FROM Test WHERE ID = @ID RETURN LEFT(@Data,LEN(@Data)-1)ENDGOThen used a simple select query to achieve the sameSELECT DISTINCT ID, [dbo].CombineData(ID) FROM TestWhich achieved the same thing without using any temporary tables and Cursors. I am sure that you will accept that this is more efficient than the one that has been published.ThanksSumanesh" |
|
robvolk
Most Valuable Yak
15732 Posts |
|
|
|
|