Concatenate multiple rows

冷暖自知 提交于 2019-11-28 14:24:24

For 2005+

SELECT CustID
     , (SELECT ca.ActionID [@value]
             , ActionName [text()]
          FROM dbo.CustAction ca
         INNER JOIN dbo.Action ON ca.ActionID = Action.ActionID
         WHERE ca.CustID = c.CustID
           FOR XML PATH('option'), TYPE) AvailableAction
  FROM dbo.Cust c

I don't think you'll be able to get this with FOR XML EXPLICIT, so if this is 2000 you're out of luck (as far as simple solutions go).

Here's a way that I'm sure will be bettered by someone that knows SQL Server XML!

 SELECT CustID  ,
       CustName,
       REPLACE(REPLACE(
       ( SELECT '<option value="' + CAST(ACTION.ActionID AS VARCHAR) + '">' + ActionName + '</option>'
       FROM    ACTION
               JOIN CustAction
               ON      CustAction.CustID = Cust.CustID
       WHERE   CustAction.ActionID       = ACTION.ActionID FOR XML PATH('')
       )
       ,'&lt;','<'),'&gt;','>') AvailableAction
FROM   Cust
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!