我有一个 xml 变量,我想将此输入 xml 分解为关系表输出,如下所示
输入
declare @x xml
set @x =
'<groupNodes>
<groupSet name="groupset1" id="10001">
<condition id="7">contains</contains>
<group name="group11" id="11">100</group>
<group name="group12" id="19">460</group>
<group name="group13" id="24">450</group>
<group name="group14" id="23">440</group>
</groupSet>
<groupSet name="groupset2" id="10002">
<condition id="3">equals</contains>
<group name="group21" id="21">370</group>
<group name="group22" id="27">543</group>
</groupSet>
<groupSet name="groupset3" id="10003">
<condition id="7">contains</contains>
<group name="group31" id="31">760</group>
<group name="group32" id="38">450</group>
<group name="group33" id="39">420</group>
..
..
</groupSet>
..
</groupNodes>'
输出
groupSetName groupSetId groupName groupId groupValue conditionId
------------- ------------ --------------------------------------
groupset1 1001 group11 11 100 7
groupset1 1001 group12 19 460 7
groupset1 1001 group13 24 450 3
groupset1 1001 group14 23 440 3
groupset2 1002 group21 21 370 3
groupset2 1002 group22 27 543 3
groupset3 1003 group31 31 760 7
groupset3 1003 group32 38 450 7
groupset3 1003 group33 39 420 7
我在下面尝试一些东西,它只给出每个子节点的第一行,而不是所有子节点都来了。另外,能够进行分组集和分组列。无法做条件节点。
select
tab.col.value('(@name)[1]', 'nvarchar(100)') groupSetName,
tab.col.value('(@id)[1]', 'nvarchar(100)') groupSetId,
tab.col.value('(./group/@name)[1]', 'nvarchar(100)') groupId,
tab.col.value('(./group/@id)[1]', 'nvarchar(100)') groupName,
tab.col.value('(./group)[1]', 'nvarchar(100)') groupValue
from @x.nodes('groupNodes/groupSet') as tab(col)
有人可以帮帮我吗。我很努力,想不通。