1

我正在尝试做的事情我认为很容易,所以如果您熟悉FOR XMLSQL Server,我建议您跳到底部并阅读粗体文本部分:)

我正在尝试使用FOR XMLSQL Server 2005 中的语句来实现我需要的结果。目前我有这个...

SELECT  
    txtReasonTypeID AS [ReasonTypeID]
  ,
    (SELECT 
    [Reason].intReasonID, 
    [Reason].txtReason
    FROM    CST_lnkProfileReason INNER JOIN 
    CST_tblReason AS [Reason] ON CST_lnkProfileReason.intReasonID = [Reason].intReasonID 
    WHERE   CST_lnkProfileReason.intProfileID = @intProfileID
    AND   CST_lnkProfileReason.txtReasonTypeID = [Response].txtReasonTypeID
    ORDER BY Reason.txtReason
    FOR XML AUTO, TYPE
    )
  ,
    (SELECT 
    [PulledSupportReason].intReasonID, 
    [PulledSupportReason].txtReason
    FROM    CST_lnkPulledSupportReason INNER JOIN 
    CST_tblReason AS [PulledSupportReason] ON CST_lnkPulledSupportReason.intReasonID = [PulledSupportReason].intReasonID 
    WHERE   CST_lnkPulledSupportReason.intProfileID = @intProfileID
    AND   CST_lnkPulledSupportReason.txtReasonTypeID = [Response].txtReasonTypeID
    ORDER BY [PulledSupportReason].txtReason
    FOR XML AUTO, TYPE
    )
FROM    CST_tblReasonTypes AS [Response]
FOR XML AUTO, ROOT('ResponseProfile')

这将返回以下 XML ...

<ResponseProfile>
  <Response ReasonTypeID="ExampleType">
    <Reason intReasonID="106" txtReason="Call Back - 1"/>
    <Reason intReasonID="147" txtReason="Call Back - 2"/>
    <PulledSupportReason intReasonID="892" txtReason="PS Reason a"/>
    <PulledSupportReason intReasonID="893" txtReason="PS Reason b"/>
  </Response>
   ...more <Response>s
</ResponseProfile>

如您所见,Reason元素和PulledSupportReason元素来自同一个表,尽管它们在此查询中是单独的元素。(可能是一个糟糕的设计案例)但是 -我想要的很简单,在ReasonandPulledSupportReason元素周围放置一个父元素,例如......

<ResponseProfile>
  <Response ReasonTypeID="ExampleType">
     <Reasons>
        <Reason intReasonID="106" txtReason="Call Back - 1"/>
        <Reason intReasonID="147" txtReason="Call Back - 2"/>
     </Reasons>
     <PulledSupportReasons>  
        <PulledSupportReason intReasonID="892" txtReason="PS Reason a"/>
        <PulledSupportReason intReasonID="893" txtReason="PS Reason b"/>
     </PulledSupportReasons>
  </Response>
   ...more <Response>s
</ResponseProfile>

XML PATH我想我可以使用or来实现这一点XML EXPLICIT?感谢您的帮助:)

4

1 回答 1

2

尝试使用FOR XML PATH('....'), ROOT('....')- 有了它,您应该能够实现您想要的。

我正在为第二个子选择说明这一点 - 对第一个子选择进行相应调整:

(SELECT 
    [PulledSupportReason].intReasonID, 
    [PulledSupportReason].txtReason
 FROM    
     CST_lnkPulledSupportReason 
 INNER JOIN 
    CST_tblReason AS [PulledSupportReason] ON CST_lnkPulledSupportReason.intReasonID = [PulledSupportReason].intReasonID 
 WHERE   
     CST_lnkPulledSupportReason.intProfileID = @intProfileID
     AND CST_lnkPulledSupportReason.txtReasonTypeID = [Response].txtReasonTypeID
 ORDER BY 
     [PulledSupportReason].txtReason
 FOR XML PATH('PulledSupportReason'), TYPE
) AS 'PulledSupportReasons'

这应该工作,我希望!(不能真正测试,因为我手头没有你的桌子可以看)

内部FOR XML PATH(PulledSupportReason')定义要使用的最里面的 XML 标记,将别名添加到整个子选择 ( AS 'PulledSupportReasons') 会为该子选择提供与定义的别名相等的包装 XML 标记。

于 2011-04-08T14:00:08.017 回答