Sql server 如何在SQL Server 2012中解析XML列?

Sql server 如何在SQL Server 2012中解析XML列?,sql-server,xml,sql-server-2012,Sql Server,Xml,Sql Server 2012,我从未在SQLServer中使用过XML解析,我希望提取自己列中的字段,获得正确的数据 在Customer表中有一个名为CustomerHeaderUncompressed的列,如下所示,如何提取SQL Server 2012中的字段和数据 <CustomerHeaderData> <CustomerHeader> <shippingmethod Value="00000000-0000-0000-0000-000000000000" Name="" /&g

我从未在SQLServer中使用过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>

0
TR
1.
2.
S3
2010-01-21
2.
假的
假的
假的
验证失败
假的
0
01/14/2010
01/01/0001

基本上是这样的:

  • 客户
    表中选择
  • 使用
    CROSS-APPLY
    和XQuery
    .nodes()
    函数获取XML作为XML片段的“动态”伪表(表别名
    XT
    ,单列别名为
    XC
  • 使用
    .value()
    XQuery函数“接触”这些XML片段并提取所需的值;使用这样的元素名称,属性需要以
    @
    符号作为前缀
尝试此方法并将其扩展到您的需要:

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)

请添加您的sql代码。以下是帮助ypi启动完美解决方案的链接。如果列值是简单xml,则不需要交叉应用。@AlecZ您能提供不交叉应用的同一查询的示例吗?@anoxis是在我的例子中,列(作为xml)在名为“data”的列中只有两个级别:对于每个所需的查询行,都有一个“”标记,其中包含所需的查询列,如“”,作为第二级标记。例如,从clienttable中选择name=data.value('(client/@name)[1],'varchar(50)')