繁体   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>

我需要将其转换为 SQL 服务器表:

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

尝试这个:

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)

您应该得到所需的 output:

在此处输入图像描述

.nodes()方法调用返回代表<ListOfContactNumber>节点的 XML 片段。 您需要进入该 XML 片段,并提取每个<ContactNumber>子节点 - 基于它们在ContactNumberTypeCoded中的值 - 然后将<ContactNumberValue>值显示为所需的 output。

暂无
暂无

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

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