0

I have written an SQL Server custom inline function which returns a table with one single column.

ALTER FUNCTION [dbo].[tblServicesByHost]
 ( @IdService int )
RETURNS table
AS
RETURN (
    SELECT dbo.viewNagiosHostGroups.NagiosHostGroups, 
      dbo.tblServicesPerHostgroup.HostGroupName, 
       dbo.tblServicesPerHostgroup.IdService
FROM          dbo.viewNagiosHostGroups INNER JOIN
                     dbo.tblServicesPerHostgroup ON dbo.viewNagiosHostGroups.NagiosHostGroups = dbo.tblServicesPerHostgroup.HostGroupName LEFT OUTER JOIN
                     dbo.tblNagiosServiceDefinitions ON dbo.tblServicesPerHostgroup.ID = dbo.tblNagiosServiceDefinitions.ID
WHERE        (dbo.tblServicesPerHostgroup.IdService = @IdService)
   )

If I call that function, it will return a table variable. How can I transform one column of that table into a comma-separated list of values, which I can then use for further processing?

aag
  • 680
  • 2
  • 12
  • 33
  • 2
    [string_agg function](https://learn.microsoft.com/en-us/sql/t-sql/functions/string-agg-transact-sql) – Lukasz Szozda Apr 01 '18 at 19:07
  • 1
    STRING_AGG is the most straight forward if you have SQL 2017. For older SQL server versions, see the following question: https://stackoverflow.com/questions/194852/how-to-concatenate-text-from-multiple-rows-into-a-single-text-string-in-sql-serv – RnP Apr 02 '18 at 07:22
  • Comma separated lists should only be used in presentation, as such, it is better to have your presentation layer handle layout like this. – HoneyBadger Apr 02 '18 at 11:19

0 Answers0