问题描述
<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 Server 表:
电话号码 | 总机th> | 电子邮件地址 | 传真号码 |
---|---|---|---|
454854 5532281 | 0987262 532281 | abc@gmail.com | 01322 296 252 |
解决方法
试试这个:
totalRows
你应该得到想要的输出:
rows
方法调用返回一个表示 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]',EmailAddress = xc.value('(ContactNumber[ContactNumberTypeCoded="EmailAddress"]/ContactNumberValue/text())[1]',FaxNumber = xc.value('(ContactNumber[ContactNumberTypeCoded="FaxNumber"]/ContactNumberValue/text())[1]','varchar(50)')
FROM
@Data.nodes('/OrderContact/Contact/ListOfContactNumber') AS XT(XC)
节点的 XML 片段。您需要访问该 XML 片段,提取每个 .nodes()
子节点 - 基于它们在 <ListOfContactNumber>
中的值 - 然后将 <ContactNumber>
值显示为所需的输出。>