鉴于此 XML 片段(VIN 显然是伪造的)
<VehicleFeed>
<Vehicle>
<VIN>1234</VIN>
<Equipment>
<EquipmentDesc>1.1</EquipmentDesc>
<EquipmentDesc>1.2</EquipmentDesc>
<EquipmentDesc>1.3</EquipmentDesc>
</Equipment>
</Vehicle>
<Vehicle>
<VIN>2345</VIN>
<Equipment>
<EquipmentDesc>2.1</EquipmentDesc>
<EquipmentDesc>2.2</EquipmentDesc>
<EquipmentDesc>2.3</EquipmentDesc>
</Equipment>
</Vehicle>
</VehicleFeed>
What I really want is this result set
VIN EquipmentDesc
--- -------------
1234 1.1
1234 1.2
1234 1.3
2345 2.1
2345 2.2
2345 2.3
我在名为@xdata 的var 中有xml 数据。下面的代码是我认为可行的,但它将所有设备值连接到单个数据列中,而不是将它们分成行。
select t.c.value('VIN[1]', 'varchar(20)') as VIN
, t1.c1.value('.', 'varchar(80)') as EquipDesc
from @xdata.nodes('//VehicleFeed/Vehicle') as t(c)
cross apply t.c.nodes('Equipment') as t1(c1)
即,我得到
VIN EquipmentDesc
--- -------------
1234 1.11.21.3
2345 2.12.22.3
我显然做错了什么,大概是交叉应用或描述数据的选择(或两者),但我不知道它是什么。看起来这很简单,但我没有看到。