set statistics io on set statistics time on SELECT distinct o.OrderID,f.Flight,f.DPort,f.APort,f.TakeOffTime,f.ArrivalTime,f.SubClass, f.Sequence,ISNULL(f.RecordNo,'')AS Pnr,o.FlightClass,c.ContactName, [value]=(select stuff((select ',' + PassengerName from O_Passenger p1 (nolock) where p1.orderid = o.OrderID for xml path('')) , 1 , 1 , ''))
FROM dbo.O_Flight f (nolock) JOIN dbo.O_Orders o (nolock) ON f.OrderID=o.OrderID JOIN dbo.O_Contact c (nolock) ON o.OrderID=c.OrderID JOIN dbo.O_Passenger p (nolock) ON p.OrderID=o.OrderID WHERE Flight='MU5870'
核心函数是stuff
[value]=(select stuff((select ',' + PassengerName from O_Passenger p1 (nolock) where p1.orderid = o.OrderID for xml path('')) , 1 , 1 , ''))
可以自己实现数据库函数列合并核心代码如下:
declare @S nvarchar(500)=''
select @S=isnull(@S,'')+isnull( FailureSource,'')+',' from OrderPayFailure
SELECT @S