简体   繁体   English

如何将 XML 数据转换为 SQL 服务器表

[英]How to convert XML data into a SQL Server table

<OrderContact>
    <Contact>
        <ContactName>Harj Dhamrait</ContactName>
        <ContactDescription>13</ContactDescription>
        <ListOfContactNumber>
            <ContactNumber>
                <ContactNumberValue />454854 5532281</ContactNumberValue>
                <ContactNumberTypeCoded>TelephoneNumber</ContactNumberTypeCoded>
            </ContactNumber>
            <ContactNumber>
                <ContactNumberValue>0987262 532281</ContactNumberValue>
                <ContactNumberTypeCoded>Other</ContactNumberTypeCoded>
                <ContactNumberTypeCodedOther>Switchboard</ContactNumberTypeCodedOther>
            </ContactNumber>
            <ContactNumber>
                <ContactNumberValue>abc@gmail.com</ContactNumberValue>
                <ContactNumberTypeCoded>EmailAddress</ContactNumberTypeCoded>
            </ContactNumber>
            <ContactNumber>
                <ContactNumberValue>01322 296 252</ContactNumberValue>
                <ContactNumberTypeCoded>FaxNumber</ContactNumberTypeCoded>
            </ContactNumber>
        </ListOfContactNumber>
    </Contact>
</OrderContact>

I need to convert this to a SQL Server table:我需要将其转换为 SQL 服务器表:

TelephoneNumber电话号码 Switchboard总机 EmailAddress电子邮件地址 FaxNumber传真号码
454854 5532281 454854 5532281 0987262 532281 0987262 532281 abc@gmail.com abc@gmail.com 01322 296 252 01322 296 252

Try this:尝试这个:

DECLARE @Data XML = '<OrderContact>
    <Contact>
        <ContactName>Harj Dhamrait</ContactName>
        <ContactDescription>13</ContactDescription>
        <ListOfContactNumber>
            <ContactNumber>
                <ContactNumberValue>454854 5532281</ContactNumberValue>
                <ContactNumberTypeCoded>TelephoneNumber</ContactNumberTypeCoded>
            </ContactNumber>
            <ContactNumber>
                <ContactNumberValue>0987262 532281</ContactNumberValue>
                <ContactNumberTypeCoded>Other</ContactNumberTypeCoded>
                <ContactNumberTypeCodedOther>Switchboard</ContactNumberTypeCodedOther>
            </ContactNumber>
            <ContactNumber>
                <ContactNumberValue>abc@gmail.com</ContactNumberValue>
                <ContactNumberTypeCoded>EmailAddress</ContactNumberTypeCoded>
            </ContactNumber>
            <ContactNumber>
                <ContactNumberValue>01322 296 252</ContactNumberValue>
                <ContactNumberTypeCoded>FaxNumber</ContactNumberTypeCoded>
            </ContactNumber>
        </ListOfContactNumber>
    </Contact>
</OrderContact>'

SELECT
    TelephoneNumber = xc.value('(ContactNumber[ContactNumberTypeCoded="TelephoneNumber"]/ContactNumberValue/text())[1]', 'varchar(50)'),
    Switchboard = xc.value('(ContactNumber[ContactNumberTypeCodedOther="Switchboard"]/ContactNumberValue/text())[1]', 'varchar(50)'),
    EmailAddress = xc.value('(ContactNumber[ContactNumberTypeCoded="EmailAddress"]/ContactNumberValue/text())[1]', 'varchar(50)'),
    FaxNumber = xc.value('(ContactNumber[ContactNumberTypeCoded="FaxNumber"]/ContactNumberValue/text())[1]', 'varchar(50)')
FROM
    @Data.nodes('/OrderContact/Contact/ListOfContactNumber') AS XT(XC)

You should get the desired output:您应该得到所需的 output:

在此处输入图像描述

The .nodes() method call returns a XML fragment representing the <ListOfContactNumber> node. .nodes()方法调用返回代表<ListOfContactNumber>节点的 XML 片段。 You need to reach into that XML fragment, and extract each <ContactNumber> child node - based on what value they have in ContactNumberTypeCoded - and then show the <ContactNumberValue> value as the desired output.您需要进入该 XML 片段,并提取每个<ContactNumber>子节点 - 基于它们在ContactNumberTypeCoded中的值 - 然后将<ContactNumberValue>值显示为所需的 output。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM