SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


A simple T-SQL statement to create a list of lookup values


A simple T-SQL statement to create a list of lookup values

Author
Message
hrc
hrc
SSC-Enthusiastic
SSC-Enthusiastic (161 reputation)SSC-Enthusiastic (161 reputation)SSC-Enthusiastic (161 reputation)SSC-Enthusiastic (161 reputation)SSC-Enthusiastic (161 reputation)SSC-Enthusiastic (161 reputation)SSC-Enthusiastic (161 reputation)SSC-Enthusiastic (161 reputation)

Group: General Forum Members
Points: 161 Visits: 52
One large advantage with XML is that the record count is .. well .. sort of unlimited.
liam.gavin
liam.gavin
SSC-Addicted
SSC-Addicted (475 reputation)SSC-Addicted (475 reputation)SSC-Addicted (475 reputation)SSC-Addicted (475 reputation)SSC-Addicted (475 reputation)SSC-Addicted (475 reputation)SSC-Addicted (475 reputation)SSC-Addicted (475 reputation)

Group: General Forum Members
Points: 475 Visits: 515
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)
stefan.hoffmann
stefan.hoffmann
SSC Rookie
SSC Rookie (29 reputation)SSC Rookie (29 reputation)SSC Rookie (29 reputation)SSC Rookie (29 reputation)SSC Rookie (29 reputation)SSC Rookie (29 reputation)SSC Rookie (29 reputation)SSC Rookie (29 reputation)

Group: General Forum Members
Points: 29 Visits: 143
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.
vevoda.ulath
vevoda.ulath
SSC Veteran
SSC Veteran (294 reputation)SSC Veteran (294 reputation)SSC Veteran (294 reputation)SSC Veteran (294 reputation)SSC Veteran (294 reputation)SSC Veteran (294 reputation)SSC Veteran (294 reputation)SSC Veteran (294 reputation)

Group: General Forum Members
Points: 294 Visits: 396
As mentioned above, wouldn't be easier to use COALESCE? You dont't nedd to worry about trailing comma.
mark.tait
mark.tait
SSC-Enthusiastic
SSC-Enthusiastic (148 reputation)SSC-Enthusiastic (148 reputation)SSC-Enthusiastic (148 reputation)SSC-Enthusiastic (148 reputation)SSC-Enthusiastic (148 reputation)SSC-Enthusiastic (148 reputation)SSC-Enthusiastic (148 reputation)SSC-Enthusiastic (148 reputation)

Group: General Forum Members
Points: 148 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
mmarovic
mmarovic
Valued Member
Valued Member (66 reputation)Valued Member (66 reputation)Valued Member (66 reputation)Valued Member (66 reputation)Valued Member (66 reputation)Valued Member (66 reputation)Valued Member (66 reputation)Valued Member (66 reputation)

Group: General Forum Members
Points: 66 Visits: 76
I wrote an article about the problem in 2006. Besides the solution with table function, look at the third solution: http://www.sql-server-performance.com/articles/dba/list_random_values_p1.aspx
liam.gavin
liam.gavin
SSC-Addicted
SSC-Addicted (475 reputation)SSC-Addicted (475 reputation)SSC-Addicted (475 reputation)SSC-Addicted (475 reputation)SSC-Addicted (475 reputation)SSC-Addicted (475 reputation)SSC-Addicted (475 reputation)SSC-Addicted (475 reputation)

Group: General Forum Members
Points: 475 Visits: 515
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.
mark.tait
mark.tait
SSC-Enthusiastic
SSC-Enthusiastic (148 reputation)SSC-Enthusiastic (148 reputation)SSC-Enthusiastic (148 reputation)SSC-Enthusiastic (148 reputation)SSC-Enthusiastic (148 reputation)SSC-Enthusiastic (148 reputation)SSC-Enthusiastic (148 reputation)SSC-Enthusiastic (148 reputation)

Group: General Forum Members
Points: 148 Visits: 23
Hi Liam - perfect, thank you - for me a scenario was all that was missing!

Cheers, Mark
j4fun
j4fun
Forum Newbie
Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)

Group: General Forum Members
Points: 6 Visits: 3
See next entry...
j4fun
j4fun
Forum Newbie
Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)

Group: General Forum Members
Points: 6 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!
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search