0

我要更新一个名为 的表Dash,我要更新的列是Analysts。从下面的代码中,我得到NameLast一个人,他们RoleId是 3 或 4。此代码将获得该人的第一次出现并将他们的姓氏插入到中,但我想要的是逗号分隔的所有人的Analysts姓氏列表3 或 4。PersonRoleId

    UPDATE Dash
    SET Dash.Analysts = Person.NameLast
    FROM dbo.Dash
    INNER JOIN dbo.Item 
    ON Dash.DPItem = Item.ItemId        
    INNER JOIN dbo.Assignment 
    ON Item.ItemId = Assignment.ItemId
    INNER JOIN dbo.Person
    ON Assignment.PersonId = Person.PersonId
    WHERE (Person.RoleId = 3 or Person.RoleId = 4)
4

1 回答 1

1
update Dash set
    Analysts = 
    stuff(
         (
             select ', ' + P.NameLast
             from dbo.Item as I
                 inner join dbo.Assignment as A on A.ItemId = I.ItemId
                 inner join dbo.Person as P on P.PersonId = A.PersonId
             where I.ItemId = D.DPItem and P.RoleId in (3, 4)
             for xml path(''), type
         ).value('.', 'nvarchar(max)')
    , 1, 2, '')
from dbo.Dash as D
于 2013-08-14T15:36:32.823 回答