Forudsat at dine data er i en tabel:
create FUNCTION toCSV (@id int)
RETURNS varchar(100)
AS
BEGIN
DECLARE @List varchar(100)
SELECT @List = COALESCE(@List + ', ', '') +
CAST(name AS varchar(10))
FROM aTable
WHERE ID = @id
RETURN(@list)
END;
go
Så:
vælg distinkt id, dbo.toCSV(id) fra en tabel