|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: Tuesday, March 13, 2012 2:31 AM
Points: 12,
Visits: 44
|
|
| One large advantage with XML is that the record count is .. well .. sort of unlimited.
|
|
|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: Today @ 6:19 AM
Points: 20,
Visits: 354
|
|
| That would definitely come in useful when extracting data from SQL, each data item could be 1000 characters in which case you would only need 8 rows before you start seeing truncation or have to start using varchar(max)
|
|
|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: Thursday, May 16, 2013 5:06 AM
Points: 21,
Visits: 94
|
|
hi,
I would either use one of both solutions:
DECLARE @Concat NVARCHAR(MAX) = '' ;
SELECT @Concat = @Concat + ', ' + StatusDesc FROM MyStatus ;
SELECT STUFF(@Concat, 1, 2, '') ;
-- or
DECLARE @Concat2 NVARCHAR(MAX) ;
SELECT @Concat2 = COALESCE(@Concat2 + ', ', '') + StatusDesc FROM MyStatus ;
SELECT @Concat2 ;
The use of COALESCE is interchangeable with ISNULL in this case. The use of SUBSTRING and LEN is not necessary when you use STUFF. Also I would not limit the length of the result variable to any arbitrary value.
The FOR XML PATH approach is in this case also viable when I only want to issue one single command or when we need to concatenate values in groups.
|
|
|
|
|
SSC-Enthusiastic
      
Group: General Forum Members
Last Login: 2 days ago @ 2:33 AM
Points: 121,
Visits: 288
|
|
| As mentioned above, wouldn't be easier to use COALESCE? You dont't nedd to worry about trailing comma.
|
|
|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Wednesday, November 21, 2012 5:02 AM
Points: 6,
Visits: 23
|
|
Hi - can I ask for a scenario for when this would be useful? I'm keen to learn.
As I see it, if I wanted a list of values - the context I would use it in, wouls be a select statement - so why not just do this:
Select myID, myField from myTable where myDesc in (Select Descriptions from DescTable)
Thanks for any clarification.
Mark
|
|
|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: Thursday, April 14, 2011 5:55 AM
Points: 16,
Visits: 76
|
|
|
|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: Today @ 6:19 AM
Points: 20,
Visits: 354
|
|
Hi Mark, two possible example scenarios:
1) You want to show data in one column that may be held in multiple rows on another table
emp id; emp name; manager(s) 1;Sally; Jack, John, Sarah, Miranda 2;John; Jack 3:Jack; null
2) To send details about in-row drop down lists to a web page where each row needs its own set of drop down items based on some business logic.
|
|
|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Wednesday, November 21, 2012 5:02 AM
Points: 6,
Visits: 23
|
|
Hi Liam - perfect, thank you - for me a scenario was all that was missing!
Cheers, Mark
|
|
|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Tuesday, March 22, 2011 11:53 AM
Points: 2,
Visits: 3
|
|
|
|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Tuesday, March 22, 2011 11:53 AM
Points: 2,
Visits: 3
|
|
Hi, somone an idea how to use as subquery it in a view like
DECLARE @mySerials NVARCHAR(max) SET @mySerials = ''
SELECT OrderNumber, Customer, City, ( SELECT @mySerials = ISNULL(@mySerials ,'') + Serial + ',' FROM Serials WHERE Serial.OrderID = Orders.ID ) AS Serials FROM Orders
The problem seems to be the equal sign near @mySerials =...
Thanks!
|
|
|
|