I have a table with two fields:ID / SURNAMEfor the same ID is possible that there are many SURNAME.I would like to have something like this:if the table have these records:1 -- SUR11 -- SUR21 -- SUR32 -- SUR42 -- SUR53 -- SUR6I would like to have.1 -- SUR1/SUR2/SUR32 -- SUR4/SUR53 -- SUR6I have 7500 records. If I use a function with COALESCE is very slow.This is my function :FUNCTION [dbo].[AllSurnames]( @Id int)RETURNS varchar(200)ASBEGIN DECLARE @Names VARCHAR(8000) SELECT @Names = COALESCE(@Names + '/ ', '') + Surname FROM T_PEOPLE WHERE (r_id_family = @Id) GROUP BY Surnamereturn @namesEND
And then I use a view:SELECT ID, dbo.AllSurnames(ID) AS SurnameFROM dbo.T_FAMILY
If I exec view I wait for 15 seconds.Any idea in order to develop and optimize this?I read that cursor is a good solution, but how can I build a cursor?