有没有办法在sql中强制结束xml daugther节点?我需要结束并重新打开一个特定的节点。我的代码目前看起来像:
USE [DBNAME]
Select (Select
'EK' as 'prices/price/group',
XUVP as 'prices/price/price',
'1' as 'prices/price/from',
'beliebig' as 'prices/price/to',
'FH' as 'prices/price/group',
XHEK as 'prices/price/price',
'1' as 'prices/price/from',
'beliebig' as 'prices/price/to',
'D' as 'prices/price/group',
XDEK as 'prices/price/price',
'1' as 'prices/price/from',
'beliebig' as 'prices/price/to',
'P' as 'prices/price/group',
XPEK as 'prices/price/price',
'1' as 'prices/price/from',
'beliebig' as 'prices/price/to'
FOR XML Path('article'),Type,Elements)
FROM [dbo].[TABLENAME] FOR XML PATH ('articles'), Elements, ROOT('Root')
GO
导致:
<prices>
<price>
<group>EK</group>
<price>459.000</price>
<from>1</from>
<to>beliebig</to>
----> </price>
----> <price>
<group>FH</group>
<price>279.000</price>
<from>1</from>
<to>beliebig</to>
</price>
</prices>
我需要在四个子注释之后“强制结束”每个价格层(第二层),并为四个节点的下一个开始新的价格层。缺少的关闭和打开标记将添加并标记在上面发布的restult中。
此外,我想为任何语法错误道歉,但我不是本地人。即使这个问题对于习惯使用sql的人来说也许是一件容易的事,我真的非常感谢你们的帮助。
提前致谢并祝福
奥托
答案 0 :(得分:1)
不确定这是否是您要查找的确切结果,但您可以通过插入NULL看到标记将重置
Declare @YourTable table (XUVP int,XHEK int,XDEK int,XPEK int)
Insert Into @YourTable values
(459,279,-999,999)
Select (Select
'EK' as 'price/group',
XUVP as 'price/price',
'1' as 'price/from',
'beliebig' as 'price/to',
null,
'FH' as 'price/group',
XHEK as 'price/price',
'1' as 'price/from',
'beliebig' as 'price/to',
null,
'D' as 'price/group',
XDEK as 'price/price',
'1' as 'price/from',
'beliebig' as 'price/to',
null,
'P' as 'price/group',
XPEK as 'price/price',
'1' as 'price/from',
'beliebig' as 'price/to'
FOR XML Path('prices'),Type,Elements, ROOT('article'))
FROM @YourTable FOR XML PATH ('articles'), Elements, ROOT('Root')
<强>返回强>
<Root>
<articles>
<article>
<prices>
<price>
<group>EK</group>
<price>459</price>
<from>1</from>
<to>beliebig</to>
</price>
<price>
<group>FH</group>
<price>279</price>
<from>1</from>
<to>beliebig</to>
</price>
<price>
<group>D</group>
<price>-999</price>
<from>1</from>
<to>beliebig</to>
</price>
<price>
<group>P</group>
<price>999</price>
<from>1</from>
<to>beliebig</to>
</price>
</prices>
</article>
</articles>
</Root>