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
 New to SQL Server Programming
 query result row referance

Author  Topic 

bobz_0585
Yak Posting Veteran

55 Posts

Posted - 2009-12-10 : 03:24:02
hello all i have a seemingly very easy problem...lets say i have a query that returns
row1
row2
row3

i want to store in a parameter(because i want to email it)
the following string
the results where row1,row2,row3 etc.. how is this achievable?

bklr
Master Smack Fu Yak Hacker

1693 Posts

Posted - 2009-12-10 : 03:30:42
use xml path()
like this
SELECT STUFF((SELECT ','+val FROM
(
SELECT 'row1' AS val UNION ALL
SELECT 'row2' UNION ALL
SELECT 'row3')s
FOR XML PATH('')),1,1,'')
Go to Top of Page

Sachin.Nand

2937 Posts

Posted - 2009-12-10 : 04:16:50
OR

declare @row as varchar(50)
set @row=''
select @row= @row + ',' + val from
(
SELECT 'row1' AS val UNION ALL
SELECT 'row2' UNION ALL
SELECT 'row3'

)t

select STUFF(@row,1,1,'')

PBUH
Go to Top of Page

bobz_0585
Yak Posting Veteran

55 Posts

Posted - 2009-12-10 : 06:18:43
ok thanks man it worked perfectly
Go to Top of Page
   

- Advertisement -