我在sql server
中有以下查询select a.*,b.first_loss_calc_result as "@firstLoss"
from tablea a
inner join tableb fl on b.id = a.ID
FOR XML AUTO, ROOT('Response')
我得到的回复是以下格式。
<Response>
<a column1="test" column 2="testh" ">
<b firstLoss="1.890600000000000e+001" />
</a>
</Response>
但我正在寻找下面提到的回复格式:
<Response>
<a column1="test" column 2="testh" firstLoss= "1.890600000000000e+001"></a>
</Response>
基本上我希望表a中的所有列和表B中的单列作为元素的属性
有人可以告诉我,我怎么能这样做。 谢谢你的帮助!
答案 0 :(得分:1)
DECLARE @TABLE1 TABLE (ID INT, column1 VARCHAR(100), Column2 VARCHAR(100))
INSERT INTO @TABLE1 VALUES
(1, 'Col1 Value1', 'Col2 Value1'),
(2, 'Col1 Value2', 'Col2 Value2')
DECLARE @TABLE2 TABLE (ID INT, FirstLoss INT)
INSERT INTO @TABLE2 VALUES
(1, 1000),(2,2000)
select A.column1 AS [@Column1]
,A.Column2 AS [@Column2]
,B.FirstLoss AS [@firstLoss]
from @TABLE1 a
inner join @TABLE2 B on b.id = a.ID
FOR XML PATH('a'), ROOT('Response')
<Response>
<a Column1="Col1 Value1" Column2="Col2 Value1" firstLoss="1000" />
<a Column1="Col1 Value2" Column2="Col2 Value2" firstLoss="2000" />
</Response>