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
 SQL Server 2008 Forums
 Transact-SQL (2008)
 , separated output

Author  Topic 

Swati Jain
Posting Yak Master

139 Posts

Posted - 2009-07-27 : 03:56:24

Hi

when order is added
table 1
ordeno, recordid
111 1

table 2
recordid serviceid service
1 1 a
1 2 b

from above table i want output as
orderno recordid service required
111 1 a,b,c

i tried the query
Declare @ServiceRequiredIDs varchar(50)
SELECT
STUFF((SELECT ',' +cast(x.[ServiceRequiredID] as varchar(10) )FROM RS_RecordDetails AS x WHERE x.[ServiceRequiredID] = a.[ServiceRequiredID] FOR XML PATH('')), 1, 1, '') as ser
From RS_RecordDetails a
where OrderNo='111'

print @ServiceRequiredIDs

select * from RS_RecordDetails where OrderNo='111'

but its not printing exact output

Pls tell me wts going wrong with the query

rajdaksha
Aged Yak Warrior

595 Posts

Posted - 2009-07-27 : 04:01:17
Hi

Try this....
DECLARE @VAR VARCHAR(MAx)
SELECT @VAR = ISNULL(@VAR+',','')+COLUMN_NAME FROM TABLE_NAME WHERE ORDERNO='111'
SELECT @VAR


-------------------------
R..
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2009-07-27 : 04:59:22
see concatenate records without UDF


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page
   

- Advertisement -