I am assuming you have parent-child tables and you are trying to get all matching children for a given parent table ID, try this function below:
CREATE FUNCTION GetCSVId(@IdParent int)
RETURNS VARCHAR(1000)
AS
BEGIN
DECLARE csv VARCHAR(1000)
SELECT @csv =COALESCE(@csv+',','') + value
from CHILDTABLE
WHERE IDParent = @IdParent
RETURN @csv
END