簡體   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