xml.node 如何获取价值
xml.node how to get value
我知道(通常)如何使用 SQL 服务器从 XML 导出数据,我已经为其余的提取完成了,我只是不知道如何获取交叉应用 XML 标签内的内容时要排列的值。
这有效并且 returns XML 标签名称
DECLARE @XML XML
SET @XML= '
<Export>
<CustomInformation name="Customer ID">12345</CustomInformation>
<CustomInformation name="Prepaid">0.00</CustomInformation>
<CustomInformation name="New Amount">0.00</CustomInformation>
</Export>
'
select
Description = CustomInformation.value('@name','nvarchar(max)')
from
@XML.nodes('/Export/CustomInformation') as b(CustomInformation)
这个returns空
DECLARE @XML XML
SET @XML= '
<Export>
<CustomInformation name="Customer ID">12345</CustomInformation>
<CustomInformation name="Prepaid">0.00</CustomInformation>
<CustomInformation name="New Amount">0.00</CustomInformation>
</Export>
'
select
Description = CustomInformation.value('@name','nvarchar(max)')
,tire_wheel = col2.value('@Prepaid', 'money')
from
@XML.nodes('/Export/CustomInformation') as b(CustomInformation)
cross apply b.CustomInformation.nodes('CustomInformation') as c(col2)
如何让数据与标签名称对齐?
DECLARE @XML XML = N'
<Export>
<CustomInformation name="Customer ID">12345</CustomInformation>
<CustomInformation name="Prepaid">120.00</CustomInformation>
<CustomInformation name="New Amount">10.00</CustomInformation>
</Export>
<Export>
<CustomInformation name="Customer ID">789</CustomInformation>
<CustomInformation name="Prepaid">160.00</CustomInformation>
<CustomInformation name="New Amount">30.00</CustomInformation>
</Export>
';
--1
select
Description = CustomInformation.value('@name','nvarchar(max)'),
Value = CustomInformation.value('.', 'nvarchar(max)')
from @XML.nodes('./Export/CustomInformation') as c(CustomInformation);
--2
select
NodeID = dense_rank() over(order by b.Export), --use this to cross tab/pivot
Description = CustomInformation.value('@name','nvarchar(max)'),
Value = CustomInformation.value('.', 'nvarchar(max)')
from @XML.nodes('./Export') as b(Export)
cross apply b.Export.nodes('./CustomInformation') as c(CustomInformation);
--3
select *
from
(
select
NodeID = dense_rank() over(order by b.Export), --use this to cross tab/pivot
Description = CustomInformation.value('@name','nvarchar(max)'),
Value = CustomInformation.value('.', 'nvarchar(max)')
from @XML.nodes('./Export') as b(Export)
cross apply b.Export.nodes('./CustomInformation') as c(CustomInformation)
) as src
pivot
(
max(value) for Description in ([Customer Id], [Prepaid], [New Amount])
) as pv;
--??
select
Description = CustomInformation.value('@name','nvarchar(max)'),
Value = CustomInformation.value('.', 'nvarchar(max)'),
[Prepaid value ?] = p.Prepaid.value('.[1]', 'nvarchar(max)')
from @XML.nodes('./Export/CustomInformation') as c(CustomInformation)
--up one level in the hierarchy...not performant
outer apply c.CustomInformation.nodes('../CustomInformation[@name="Prepaid"]') as p(Prepaid);
--??
select
Description = CustomInformation.value('@name','nvarchar(max)'),
Value = CustomInformation.value('.', 'nvarchar(max)'),
[Prepaid value ?] = p.Prepaid.value('.[1]', 'nvarchar(max)')
from @XML.nodes('./Export') as b(Export)
cross apply b.Export.nodes('./CustomInformation') as c(CustomInformation)
outer apply b.Export.nodes('./CustomInformation[@name="Prepaid"]') as p(Prepaid);
我知道(通常)如何使用 SQL 服务器从 XML 导出数据,我已经为其余的提取完成了,我只是不知道如何获取交叉应用 XML 标签内的内容时要排列的值。
这有效并且 returns XML 标签名称
DECLARE @XML XML
SET @XML= '
<Export>
<CustomInformation name="Customer ID">12345</CustomInformation>
<CustomInformation name="Prepaid">0.00</CustomInformation>
<CustomInformation name="New Amount">0.00</CustomInformation>
</Export>
'
select
Description = CustomInformation.value('@name','nvarchar(max)')
from
@XML.nodes('/Export/CustomInformation') as b(CustomInformation)
这个returns空
DECLARE @XML XML
SET @XML= '
<Export>
<CustomInformation name="Customer ID">12345</CustomInformation>
<CustomInformation name="Prepaid">0.00</CustomInformation>
<CustomInformation name="New Amount">0.00</CustomInformation>
</Export>
'
select
Description = CustomInformation.value('@name','nvarchar(max)')
,tire_wheel = col2.value('@Prepaid', 'money')
from
@XML.nodes('/Export/CustomInformation') as b(CustomInformation)
cross apply b.CustomInformation.nodes('CustomInformation') as c(col2)
如何让数据与标签名称对齐?
DECLARE @XML XML = N'
<Export>
<CustomInformation name="Customer ID">12345</CustomInformation>
<CustomInformation name="Prepaid">120.00</CustomInformation>
<CustomInformation name="New Amount">10.00</CustomInformation>
</Export>
<Export>
<CustomInformation name="Customer ID">789</CustomInformation>
<CustomInformation name="Prepaid">160.00</CustomInformation>
<CustomInformation name="New Amount">30.00</CustomInformation>
</Export>
';
--1
select
Description = CustomInformation.value('@name','nvarchar(max)'),
Value = CustomInformation.value('.', 'nvarchar(max)')
from @XML.nodes('./Export/CustomInformation') as c(CustomInformation);
--2
select
NodeID = dense_rank() over(order by b.Export), --use this to cross tab/pivot
Description = CustomInformation.value('@name','nvarchar(max)'),
Value = CustomInformation.value('.', 'nvarchar(max)')
from @XML.nodes('./Export') as b(Export)
cross apply b.Export.nodes('./CustomInformation') as c(CustomInformation);
--3
select *
from
(
select
NodeID = dense_rank() over(order by b.Export), --use this to cross tab/pivot
Description = CustomInformation.value('@name','nvarchar(max)'),
Value = CustomInformation.value('.', 'nvarchar(max)')
from @XML.nodes('./Export') as b(Export)
cross apply b.Export.nodes('./CustomInformation') as c(CustomInformation)
) as src
pivot
(
max(value) for Description in ([Customer Id], [Prepaid], [New Amount])
) as pv;
--??
select
Description = CustomInformation.value('@name','nvarchar(max)'),
Value = CustomInformation.value('.', 'nvarchar(max)'),
[Prepaid value ?] = p.Prepaid.value('.[1]', 'nvarchar(max)')
from @XML.nodes('./Export/CustomInformation') as c(CustomInformation)
--up one level in the hierarchy...not performant
outer apply c.CustomInformation.nodes('../CustomInformation[@name="Prepaid"]') as p(Prepaid);
--??
select
Description = CustomInformation.value('@name','nvarchar(max)'),
Value = CustomInformation.value('.', 'nvarchar(max)'),
[Prepaid value ?] = p.Prepaid.value('.[1]', 'nvarchar(max)')
from @XML.nodes('./Export') as b(Export)
cross apply b.Export.nodes('./CustomInformation') as c(CustomInformation)
outer apply b.Export.nodes('./CustomInformation[@name="Prepaid"]') as p(Prepaid);