我有一个从表中返回多行的查询。然后我将该查询转换为此查询:
;with mycte as
(select s.FirstName + ' ' + s.LastName as Name from ClientStaff cs
left outer join Staff s on s.Id = cs.StaffId
left outer join GeneralStatus gs on gs.Id = s.StatusId
where cs.ClientId = @clientId and gs.Name = 'Active')
select @staff = (select distinct staff = REPLACE(REPLACE(REPLACE((select Name AS [data()] FROM mycte a
order by a.Name for xml path),'</row><row>',', '),'</row>',''),'<row>','') from mycte b)
它在一个以逗号分隔的行中返回这些行。 现在我不想要逗号分隔值,而是想要单行分隔值。 谁能告诉我是否有可能?
提前致谢。
答案 0 :(得分:3)
declare @staff varchar(max)
;with mycte as
(
select distinct s.FirstName + ' ' + s.LastName as Name
from ClientStaff cs
left outer join Staff s on
s.Id = cs.StaffId
left outer join GeneralStatus gs on
gs.Id = s.StatusId
where cs.ClientId = @clientId and gs.Name = 'Active'
)
select @staff = isnull(@staff + char(13), '') + Name
from mycte b
print @staff