我需要以下XML结构
linenumber TEST TEST2 TEST3
1 valu value value
我使用的表
<root>
<lkz>12345</lkz>
<styles>
<style>
<productNo>
..
</productNo>
</style>
<style>
<productNo>
..
</productNo>
</style>
</styles>
</root>
lkz | productNo
------+----------------
12345 | 123456-789
12345 | 1213456-788
始终相同
我的T-SQL代码:
lkz
结果
DECLARE @xmlData XML
SET @xmlData = (SELECT productNo
FROM dbo.otto
FOR XML PATH ( 'Style' ), ROOT ('Styles'), ELEMENTS)
SELECT @xmlData
问题(对不起我的英语):
我如何得到&#34;一切&#34;
如何将<Styles>
<Style>
<productNo>123456-789</productNo>
</Style>
<Style>
<productNo>123456-788</productNo>
</Style>
</Styles>
仅作为简单节点的第二行
感谢您的帮助!
答案 0 :(得分:2)
在主查询中按lkz
分组,并在子查询中获取相应的productNo
值。
select O1.lkz,
(
select O2.productNo
from dbo.Otto as O2
where O1.lkz = O2.lkz
for xml path('style'), root('styles'), type
)
from dbo.otto as O1
group by O1.lkz
for xml path(''), root('root');
答案 1 :(得分:0)
我的桌子
lkz|stylesNo|ClassOfGoods|ClusterAttribute1
12345|123456-789|test1|Herren
12345|123456-788|test2|Damen
需要xml
<root>
<LKZ>12345</LKZ>
<Styles>
<Style>
<StyleNo>123456-789</StyleNo>
<ClassOfGoods>test1</ClassOfGoods>
<ClusterAttribute1 CountryIsoCode="DE"><![CDATA[Herren]]></ClusterAttribute1>
我的t-sql
SET @xmlData =
(select O1.lkz,
(
select O2.stylesNo
,O2.ClassOfGoods
,(SELECT
1 as Tag,
0 as Parent,
'DE' as [ClusterAttribute1!1!CountryIsoCode],
ClusterAttribute1 as [ClusterAttribute1!1!!CDATA]
FROM
dbo.otto as O3
--where O1.stylesNo = O3.stylesNo
FOR XML EXPLICIT, type
)
from dbo.otto as O2
where O1.lkz = O2.lkz
for xml path('style'), root('styles'), type
)
from dbo.otto as O1
group by O1.lkz
for xml path(''), root('ottopartner')
)
SELECT @xmlData
结果
<root>
<lkz>12345</lkz>
<styles>
<style>
<stylesNo>123456-789</stylesNo>
<ClassOfGoods>test1</ClassOfGoods>
<ClusterAttribute1 CountryIsoCode="DE">Herren</ClusterAttribute1>
<ClusterAttribute1 CountryIsoCode="DE">Damen</ClusterAttribute1>
</style>
<style>
<stylesNo>123456-788</stylesNo>
<ClassOfGoods>test2</ClassOfGoods>
<ClusterAttribute1 CountryIsoCode="DE">Herren</ClusterAttribute1>
<ClusterAttribute1 CountryIsoCode="DE">Damen</ClusterAttribute1>
</style>
</styles>
</root>