I\'m writing an export function, where I need to export contacts to Excel, and I\'ve run into a technical snag - or perhaps a gap in my SQL skills is closer to the truth. ;)
Try this
declare @Roles nvarchar(max)
select @Roles = case when @Roles is null then '' else @Roles + ', ' end + Role.Name
from Role
inner join ContactRole on Role.ID = ContactRole.RoleID
where ContactRole.ContactID = @ContactID
select @Roles
update:
Above code covers functionality for a single contact. You can create a scalar function with parameter @ContactID and call the function from a
Select Name, dbo.GetContactRoles(ID) From Contact