|
|
|
SSC-Enthusiastic
      
Group: General Forum Members
Last Login: Friday, September 07, 2012 2:35 PM
Points: 147,
Visits: 46
|
|
|
|
|
|
Old Hand
      
Group: General Forum Members
Last Login: Thursday, May 16, 2013 7:15 PM
Points: 350,
Visits: 349
|
|
You can use the following SQL to compose the comma-delimited string, without needed to trim the trailing comma at the end:
SELECT @MyStatusList = ISNULL(@MyStatusList + ',', '') + StatusDesc FROM (SELECT DISTINCT StatusDesc FROM MyStatus) x
|
|
|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Monday, March 21, 2011 4:48 AM
Points: 5,
Visits: 22
|
|
It can be made simple with the following sql statement
SELECT REPLACE((SELECT DISTINCT RTRIM(LTRIM(StatusDesc)) AS 'data()' FROM dbo.MyStatus FOR XML PATH ( '' )), ' ', ', ')
|
|
|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Thursday, December 13, 2012 6:19 AM
Points: 1,
Visits: 42
|
|
Yo can also write example like this:
DECLARE @MyStatusList VARCHAR(1000) SELECT @MyStatusList = COALESCE(@MyStatusList + ',', '') + StatusDesc FROM MyStatus select @MyStatusList
|
|
|
|
|
SSC Eights!
      
Group: General Forum Members
Last Login: Today @ 1:30 AM
Points: 803,
Visits: 2,124
|
|
Cursors in MS T-SQL should be avoided unless absolutely necessary as they are extremely slow and inefficent.
Using one of the methods mentioned in previous posts is a better and more efficent alternative to the method you mention, my personal preference is the XML conversion with a nested Replace.
_________________________________________________________________________ SSC Guide to Posting and Best Practices
|
|
|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: Saturday, June 30, 2012 12:20 PM
Points: 14,
Visits: 63
|
|
In regards distinct values:
SELECT @s = ISNULL(@s+',','')+field FROM dbo.TABLE GROUP BY field
works well too.
|
|
|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: Saturday, June 30, 2012 12:20 PM
Points: 14,
Visits: 63
|
|
arty 15255 (3/21/2011) In regards distinct values:
SELECT @s = ISNULL(@s+',','')+field FROM dbo.TABLE GROUP BY field
works well too.
I forgot to mention that ORDER BY field works as well :)
|
|
|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: Tuesday, March 13, 2012 2:31 AM
Points: 12,
Visits: 44
|
|
Jason: May I ask for a a short example on how to do XML with nested replaces? I have no experience with XML in SQL. Okay. Found it, don't understand it though. 5 minutes later I understand it. A better solution because there's no record limit
|
|
|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: Tuesday, March 13, 2012 2:31 AM
Points: 12,
Visits: 44
|
|
Jason: May I ask for a a short example on how to do XML with nested replaces? I have no experience with XML in SQL. Okay. Found it, don't understand it though
|
|
|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: Today @ 6:19 AM
Points: 20,
Visits: 354
|
|
| Jason, the method described does not use a cursor. It is good to share the method in the article but it should be known and in use already which judging by posts it is. Comparing the execution plan for XML version and article version shows SQL handling the query in exactly the same way, so no benefit from using XML in this instance that I can see. Might be worthwhile exploring XML though if requirements were more complex.
|
|
|
|