Here the code from my function:
-- Get Country Names
DECLARE getCountriesName CURSOR FOR
SELECT c.Name
FROM VocabCountry c
RIGHT OUTER JOIN ProjectCountryRelations cr
ON cr.CountryId = c.Id
WHERE
c.Id = @project_id;
-- Finally Create list to return
OPEN getCountriesName;
FETCH NEXT FROM getCountriesName INTO @Name;
WHILE @@FETCH_STATUS = 0
BEGIN
SET @listOfItems = @listOfItems + @Name + ', '
FETCH NEXT FROM getCountriesName INTO @Name;
END;
CLOSE getCountriesName;
DEALLOCATE getCountriesName;
I am expecting a list of comma separated values, for example, like so:
Canada, United States of America,
I verified that the SELECT returns the countries expected.
Thanks for any help!
Eric
If you’re on SQL Server 2008 or newer, you could easily do this with a single SELECT and some
FOR XML PATHmagic 🙂That should return a comma-separated list of those country names for you. No ugly and awful cursor needed!