私がやろうとしていることは非常に簡単だと思うのでFOR XML
、SQL Serverに精通している場合は、一番下までスキップして太字のテキスト部分を読むことをお勧めします:)
FOR XML
SQL 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
(おそらく悪いデザインの場合)しかし、私が欲しいのは、親要素を要素Reason
とPulledSupportReason
要素の周りに配置するのに十分単純です。
<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
かXML EXPLICIT
?助けてくれてありがとう:)