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
 Script Library
 More efficient way than you have published

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)
AS
BEGIN
DECLARE @Data VARCHAR(2000)

SET @Data = ''
SELECT @Data = @Data + Data + ',' FROM Test WHERE ID = @ID
RETURN LEFT(@Data,LEN(@Data)-1)

END
GO

Then used a simple select query to achieve the same

SELECT DISTINCT ID, [dbo].CombineData(ID) FROM Test

Which 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.


Thanks
Sumanesh"

robvolk
Most Valuable Yak

15732 Posts

Posted - 2004-12-08 : 08:05:43
I'm sure we'll accept that we published this solution (and others) 2 years ago:

http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=19647
http://www.sqlteam.com/item.asp?ItemID=11021
Go to Top of Page
   

- Advertisement -