1

我无法将 XML 属性“Country”的值放入我的表中。

我究竟做错了什么?

这是我的 XML:

<?xml version="1.0" encoding="utf-8"?>
<CustomerDetails>
      <PersonalInfo Country="USA">
          <CustID>1001</CustID>
          <CustLastName>Smith</CustLastName>
          <DOB>2011-05-05T09:25:48.253</DOB>
          <Address>
            <Addr1>100 Smith St.</Addr1>
            <City>New York</City>
          </Address> 
      </PersonalInfo>   
</CustomerDetails>

这是我的 SQL:

Drop table #Cust
CREATE TABLE #Cust
    (CustID INT, CustLastName VARCHAR(10)
             , DOB DATETIME, Addr1 VARCHAR(100), City VARCHAR(10), Country VARCHAR(20))
insert into #Cust
select
   c3.value('CustID[1]','int'),
   c3.value('CustLastName[1]','varchar(10)'),
   c3.value('DOB[1]','DATETIME'),
   c3.value('(Address/Addr1)[1]','VARCHAR(100)'),
   c3.value('(Address/City)[1]','VARCHAR(10)'),
   c3.value('Country[1]','VARCHAR(20)')
from
(
   select 
      cast(c1 as xml)
   from 
      OPENROWSET (BULK 'C:\Users\wattronts\Documents\XMLImportTest.xml',SINGLE_BLOB) as T1(c1)
)as T2(c2)
cross apply c2.nodes('/CustomerDetails/PersonalInfo') T3(c3)

Select * from #Cust

谢谢你的帮助。

4

1 回答 1

2

用于@指定您需要一个属性。

T3.c3.value('@Country', 'varchar(50)')
于 2013-03-26T14:12:36.740 回答