我正在使用coalesce来返回一串名字,当我把它作为单个查询运行时,我没有问题,但是当我想把它放在一个更大的查询中时,我有点难过。 / p>
以下是可行的:
DECLARE @return varchar(200)
SELECT @return = COALESCE(@return + ', ', '') +
CAST((select FileAs) AS varchar(30))
from Objects
where ObjectId in (select objectid from dbo.Get_XML_Links(2335))
select @return
但我想将名称列表添加到更大的查询中,如下所示:
select det.Description,
StartTime,
EndTime,
THE STRING OF NAMES
from DiaryEvents de
inner join DiaryEventTypes det on det.DiaryEventTypeID = de.EventTypeID
where EventTypeID in (29,40)
我无法完全看到我需要做什么(或者是否可以采用这种格式!)
答案 0 :(得分:0)
感谢上面的评论以及关于SQL Authority的非常方便的文章,我的工作方式如下:
select det.Description,
StartTime,
EndTime,
(
SELECT SUBSTRING(
(select ',' + FileAs
from Objects
where ObjectId in (select objectid from dbo.Get_XML_Links(de.diaryeventid) where ObjectTypeSystemCode = 'CCT')
FOR XML PATH('')),2,200000)
)AS Contacts
from DiaryEvents de
inner join DiaryEventTypes det on det.DiaryEventTypeID = de.EventTypeID
where EventTypeID in (29,40)