我想更新名为Dash
的表,我要更新的列名为Analysts
。从下面的代码中我得到一个NameLast
个人RoleId
为3或4的人。此代码将首次出现在该人身上并将其姓氏插入Analysts
,但我想要的是所有Person
RoleId
3或4的逗号分隔的姓氏列表。
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)
答案 0 :(得分: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