0
我想從SQL查詢中格式化一些XML輸出,並且很掙扎。我目前擁有的SQL是類似這樣的事情,我已經改變了表和列的名稱:
SELECT *
FROM ReportTable p with (nolock)
LEFT OUTER JOIN Table1 s with (nolock) on s.UID = p.UID
LEFT OUTER JOIN Table2 o with (nolock) on o.UID = p.UID
LEFT OUTER JOIN Table3 ct with (nolock) on ct.UID = p.UID
WHERE p.UID = 1194
FOR XML AUTO, ELEMENTS
,這是給我下面的輸出:
<p>
<UID>1194</UID>
<pCol1>Value</pCol1>
<pCol2>Value</pCol2>
<pCol3>Value</pCol4>
<pCol4>Value</pCol4>
<s>
<UID>1194</UID>
<sCol1>Value</sCol1>
<sCol2>Value</sCol2>
<sCol3>Value</sCol3>
<o>
<ct>
<UID>1194</UID>
<ctCol1>Value</ctCol1>
<ctCol2>Value</ctCol2>
</ct>
</o>
</s>
</p>
這是不是什麼我之後,我真正需要的是這樣的:
<p>
<UID>1194</UID>
<pCol1>Value</pCol1>
<pCol2>Value</pCol2>
<pCol3>Value</pCol4>
<pCol4>Value</pCol4>
<s>
<UID>1194</UID>
<sCol1>Value</sCol1>
<sCol2>Value</sCol2>
<sCol3>Value</sCol3>
</s>
<o>
</o>
<ct>
<UID>1194</UID>
<ctCol1>Value</ctCol1>
<ctCol2>1</ctCol2>
</ct>
</p>
難道只是我必須改變我的SQL所以它使用的子查詢,或者有我錯過了什麼令人訝異明顯與我的SQL的情況下?
理想情況下,該解決方案還需要適用於從2000年開始的SQL Server版本。
謝謝。