I was checking that using this is good to handle special characters but at the same time is over complicating the query generating a "cardinality estimate warning"
If I use FOR XML PATH(''), the query plan is much better and the cardinatity is gone. Anybody faced this issue before? is there any workaround to continue using FOR XML PATH, TYPE).value('.[1]','nvarchar(max)') and get rid of the cardinality issue?
SELECT r.ServiceId,
STUFF(
(
SELECT '; ' + u.Name
FROM dbo.UsedFor u
inner join dbo.ServiceUsedRelation r2
on u.UsedId = r2.UsedId
where
r2.ServiceId = r.ServiceId
FOR XML PATH, TYPE).value('.[1]','nvarchar(max)')
, 1
, 1
, ''
) as Name
FROM dbo.ServiceUsedRelation r
GROUP BY r.ServiceId