TSQL Comma Separation

前端 未结 7 1915
夕颜
夕颜 2020-12-10 20:12

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. ;)

7条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-10 20:49

    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
    

提交回复
热议问题