你需要的是SQL Join 和 concatenate rows,关于它有很多问题。在 SQL Server 中没有简单的方法可以做到这一点,但这里有一些技巧:
使用select for xml 进行连接
select
app.subject,
stuff(
(
select ', ' + AL.locationName
from appointmentlocation as AL
where AL.appointment = app.activityid
for xml path(''), type
).value('.', 'nvarchar(max)')
, 1, 2, '')
from FilteredAppointment as app
where app.scheduledstart='2013-07-06 15:00:00.000'
如果您只有一条来自 FilteredAppointment 的记录要连接,则可以使用聚合到变量中:
declare @locations nvarchar(max), @activityid int
select @activityid = ???
select @locations = isnull(@locations + ', ', '') + AL.locationName
from appointmentlocation as AL
where AL.appointment = @activityid
print @locations