【发布时间】:2016-01-07 17:32:49
【问题描述】:
我有桌子
Cost RenderedValue SectionName
------------------------------------
100.00 1000.00 Section1
200.00 2000.00 Section2
300.00 3000.00 Section3
400.00 4000.00 Section4
我想生成这个 XML:
<Root>
<Section1Cost>100.00</Section1Cost>
<Section1RenderedValue>1000.00</Section1RenderedValue>
<Section2Cost>200.00</Section2Cost>
<Section2RendredValue>2000.00</Section2RendredValue>
</Root>
我能够使用 TSQL 生成它(Test 是我的表名)
SELECT
(SELECT Cost AS 'Cost'FROM Test WHERE SectionName = 'Section1') AS 'Section1Cost',
(SELECT RenderedValue AS 'Cost'FROM Test WHERE SectionName = 'Section1') AS 'Section1RenderedValue',
(SELECT Cost AS 'Cost'FROM Test WHERE SectionName = 'Section2') AS 'Section2Cost',
(SELECT RenderedValue AS 'Cost'FROM Test WHERE SectionName = 'Section2') AS 'Section2RendredValue'
FOR XML Path(''), Root('Root')
但这很丑,我认为它没有优化。它可以更优雅还是我拥有的任何东西都是正确的?
我在那个测试表中最多可以有 30 行
【问题讨论】:
-
请注意,所有在 SO 上提供答案的专业人士都渴望获得声誉积分。 Please read this: someone-answers。也可以看看您的旧问题。谢谢!
标签: sql-server xml tsql for-xml