[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

microsoft.public.sqlserver.programming

Grouped String Concatenation

Ion Freeman

3/7/2007 9:21:00 PM

So, say I have a situation like
create table #mable(mid INT, token nvarchar(16))
INSERT INTO #mable(0, 'foo')
INSERT INTO #mable(0, 'goo')
INSERT INTO #mable(1, 'hoo')
INSERT INTO #mable(1, 'moo')

And I want a resultset like
0 foo, goo
1 hoo, moo

I know I can get foo, goo, hoo, moo with
DECLARE @oowords nvarchar(31)
SELECT @oowords = ISNULL(@oowords + ', ', space(1)) + token FROM
#mable ORDER BY token

but it's not clear to me how I'd break it up by mid without a cursor.

Any ideas? I'm going to putter forward with the cursor, but I'd love
to be able to drop it.

3 Answers

Anith Sen

3/7/2007 9:25:00 PM

0

Ion Freeman

3/7/2007 9:44:00 PM

0

Anith,
Wouldn't I have to be able to access #mable from within a UDF?
Accessing the source data from within the UDF was slow -- I'm in the
middle of reintegrating the function back into my main stored
procedure.
Ion

On Mar 7, 4:24 pm, "Anith Sen" <a...@bizdatasolutions.com> wrote:
> Try:http://www.projectdmx.com/tsql/rowconcat...
>
> --
> Anith


Anith Sen

3/7/2007 9:59:00 PM

0

Not sure which UDF you are referring to, but using one of the methods
described in the link I posted, you could write :

SELECT m1.mid,
( SELECT m2.token + ','
FROM #mable m2
WHERE m2.mid = m1.mid
ORDER BY token
FOR XML PATH('') ) AS token
FROM #mable m1
GROUP BY m1.mid ;

--
Anith