Hi @Bart K ,
As mentioned by Erland, you should fix your parameters when you create one User-defined function.
You could also check whether there is any 'BEGIN' or 'END' missing in your procedure.
Per my knowledge, you could simplely use "STUFF(COLUMN FOR XML PATH('')" instead of "string_agg" and use "CONCAT" instead of "concat_ws" on MSSQL 2016 and earlier.
Please refer one example from below:
DECLARE @A_TEMP_TABLE_USED_AS_EXAMPLE TABLE(CountryCode INT, City VARCHAR(50))
INSERT @A_TEMP_TABLE_USED_AS_EXAMPLE(CountryCode, City) VALUES
(1, 'Johannesburg'), (1, 'Cape Town'), --South Africa
(2, 'New York'), (2, 'Washington'), --USA
(3, 'Paris') ,(3, 'Nice'), --France
(4, 'Rome'), (4, 'Bologna'), --Itlay
(5, 'Athens'), (5, 'Volos') --Greece
SELECT
CountryCode,
STRING_AGG(city,',')
FROM @A_TEMP_TABLE_USED_AS_EXAMPLE
GROUP BY CountryCode
SELECT DISTINCT
CountryCode,
STUFF((SELECT ',' + City
FROM @A_TEMP_TABLE_USED_AS_EXAMPLE [TABLE1]
WHERE TABLE1.CountryCode = TABLE2.CountryCode
FOR XML PATH('')), 1, 1,'')
FROM @A_TEMP_TABLE_USED_AS_EXAMPLE [TABLE2]
SELECT DISTINCT CONCAT_WS(',',CountryCode,City)
FROM @A_TEMP_TABLE_USED_AS_EXAMPLE
SELECT DISTINCT
CONCAT(CountryCode,',',City)
FROM @A_TEMP_TABLE_USED_AS_EXAMPLE
If the response is helpful, please click "Accept Answer" and upvote it.
Best regards
Melissa