【问题标题】:How to parse XML column in SQL Server 2012?如何解析 SQL Server 2012 中的 XML 列?
【发布时间】:2016-11-09 20:56:53
【问题描述】:

我从来没有在 SQL Server 中使用过 XML 解析,我想在自己的列中提取字段,得到正确的数据。

我在Customer 表中有一个名为CustomerHeaderUncompressed 的列,如下所示,如何在 SQL Server 2012 中提取字段和数据?

<CustomerHeaderData>
<CustomerHeader>
    <shippingmethod Value="00000000-0000-0000-0000-000000000000" Name="" />
    <discount Value="" />
    <customdiscount Value="0" />
    <ponumber Value="9909933793" />
    <tax1 Value="-1" />
    <tax2 Value="-1" />
    <tax3 Value="0" />
    <tax3name Value="" />
    <tax4 Value="0" />
    <Freight />
    <ClientExtraField6 Value="5" />
    <ClientExtraField7 Value="3" />
    <dateneeded Value="01/01/0001 00:00:00" />
    <ClientTaxCodeSource>0</ClientTaxCodeSource>
    <shippingbranch />
    <dropnumber Value="" />
    <comment Value="" />
    <shippingzone Value="" />
    <salespersonID Value="704e78d4-cdbb-4963-bcc2-2c83a1d5f3fd" />
    <salesperson Value="Salesrep, XYZ" />
    <installation Value="False" />
    <salesterms Value="18" />
    <HeldItemDeliveryMethod Value="0" />
    <customcontrol>
        <CustomCustomerHeader CultureInfo="en-US">
            <BusinessSegment>TR</BusinessSegment>
            <BusinessSegmentID>1</BusinessSegmentID>
            <OrderType>2</OrderType>
            <MarketSegment>S3</MarketSegment>
            <CustomerDeliveryDate>2010-01-21</CustomerDeliveryDate>
            <BuildingPermitNumber />
            <FinalWallDepth />
            <PricingType>2</PricingType>
            <HouseBuiltBefore1978>False</HouseBuiltBefore1978>
            <AttributePricing>False</AttributePricing>
            <UndeterminedAttributes>False</UndeterminedAttributes>
            <EventIDStatus>VerifyFailed</EventIDStatus>
            <EventIDEnabled>False</EventIDEnabled>
            <CustomerDiscount>0</CustomerDiscount>
            <PreparedBy />
            <RequestedShipDate>01/14/2010</RequestedShipDate>
            <UserTestDate>01/01/0001</UserTestDate>
        </CustomCustomerHeader>
    </customcontrol>
</CustomerHeader>

【问题讨论】:

标签: sql-server xml sql-server-2012


【解决方案1】:

基本上是这样的:

  • 从您的Customer 表中选择
  • 使用CROSS APPLY 和XQuery .nodes() 函数将XML 抓取为XML 片段的“即时”伪表(表别名XT,单列别名为XC
  • 使用.value() XQuery 函数“到达”这些 XML 片段并提取您需要的值;使用 element 名称,并且 attributes 需要以 @ 符号为前缀

试试这个并将其扩展到您的需要:

SELECT 
    ShippingMethodValue = XC.value('(shippingmethod/@Value)[1]', 'varchar(50)'),
    ShippingMethodName = XC.value('(shippingmethod/@Name)[1]', 'varchar(50)'),
    DiscountValue = XC.value('(discount/@Value)[1]', 'varchar(50)'),
    CustomDiscountValue = XC.value('(customdiscount/@Value)[1]', 'varchar(50)'),
    PONumber= XC.value('(ponumber/@Value)[1]', 'bigint' )
FROM
    Customer
CROSS APPLY
    CustomerHeaderUncompressed.nodes('/CustomerHeaderData/CustomerHeader') AS XT(XC)

【讨论】:

  • 完美解决方案。如果列值是简单的 xml,则不需要交叉应用。
  • @AlecZ 你能提供没有交叉应用的相同查询的例子吗?
  • @anoxis 是的,在我的情况下,一列(如 xml)在名为“data”的列中只有两个级别:对于每个所需的查询行,都有一个带有所需的“”标签查询诸如“”之类的列作为二级标签。所以例如SELECT name = data.value('(client/@name)[1]', 'varchar(50)') FROM clienttable
猜你喜欢
  • 2013-02-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-18
  • 1970-01-01
相关资源
最近更新 更多