Click here to Skip to main content
15,909,939 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I need to combine multiple rows values into single row with comma separater (,). i had got example from internet and implemented but still some errors exists

select distinct ', ' + TempGroupEmailEmp.EmpName from TempGroupEmailEmp


executing above query displays rows values starting with comma

select STUFF((select distinct ', ' + TempGroupEmailEmp.EmpName from TempGroupEmailEmp),1,2,'') as tmp from TempGroupEmailEmp


for the above query displays the following error

Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.


any suggestions

Thanks in advance
Posted

it works i missed "for xml path ('')" in the query

correct query as follows

select distinct STUFF((select distinct ', ' + TempGroupEmailEmp.EmpName from TempGroupEmailEmp for XML path('')),1,1,'') as tmp from TempGroupEmailEmp
 
Share this answer
 
Don't use STUFF:
SQL
SELECT SUBSTRING((SELECT DISTINCT ', ' + TempGroupEmailEmp.EmpName FROM TempGroupEmailEmp FOR XML PATH('')),2,100000) AS CSV
 
Share this answer
 
STUFF works on a single row, but you're feeding it all the rows selected in the inner select.

Try replacing it with;
select STUFF(', ' + TempGroupEmailEmp.EmpName,1,2,'') as tmp from TempGroupEmailEmp


(Or, better yet, don't prefix the EmpName with ', ' just to then remove it using STUFF.)

Hope this helps,
Fredrik
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900