0

I am having trouble with returning multiple values in a sub query and adding them to a string. Currently I am using:

Select master_key, (select parent_key 
                    from cmpower.dbo.dlnk 
                   where dlnk.parent_key = sbrv_table.masterkey) 
from cmpower.dbo.sbrv_table 

The sub query returns multiple rows which I need to put into a comma delimited string.

I am not sure how to add the sub query results to a string to be returned with the main select query.

Any guidance would be much appreciated.

War10ck
  • 12,387
  • 7
  • 41
  • 54
  • possible duplicate of [Simulating group\_concat MySQL function in Microsoft SQL Server 2005?](http://stackoverflow.com/questions/451415/simulating-group-concat-mysql-function-in-microsoft-sql-server-2005) – Sam DeHaan Apr 02 '14 at 20:48
  • Did you see [this post][1] It is the easiest I think. [1]: http://stackoverflow.com/questions/455423/how-to-turn-one-column-of-a-table-into-a-csv-string-in-sql-server-without-using – thetimmer Apr 02 '14 at 20:55

1 Answers1

0
SELECT sbrv_table.master_key 
    , STUFF((select ', ' + parent_key [text()]
              from dbo.dlnk 
              where dlnk.parent_key = sbrv_table.masterkey
              FOR XML PATH(''),TYPE)
             .value('.','NVARCHAR(MAX)'),1,2,'') AS parent_key_List                      
FROM dbo.sbrv_table 
GROUP BY sbrv_table.master_key  
M.Ali
  • 67,945
  • 13
  • 101
  • 127