【问题标题】:xml.node how to get valuexml.node 如何获取值
【发布时间】:2020-04-10 00:53:39
【问题描述】:

我(通常)知道如何使用 SQL Server 从 XML 中导出数据,并且我已经在提取的其余部分这样做了,我只是不知道如何在交叉应用这些东西时让这些值对齐在 XML 标记内。

这有效并返回 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)

这将返回 null

 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)

如何让数据与标签名对齐?

【问题讨论】:

    标签: sql-server ssms-2017


    【解决方案1】:
    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); 
    

    【讨论】:

    • 太棒了,谢谢。这 '。'对我来说是缺失的环节。感谢您的回复,并已将其标记为答案。
    猜你喜欢
    • 2019-10-28
    • 2012-04-13
    • 2021-03-27
    • 2012-10-20
    • 2011-11-19
    • 2016-08-17
    • 2015-08-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多