2017-07-29 114 views
1

用下面的代碼我試圖將一個xml文檔引入到SQL Server Management Studio中。代碼運行,但在結果頁面中,行數據全部爲NULL。下面是代碼:查詢SQL Server 2014中的XML數據

declare @xml xml 

select @xml=d 
from openrowset (bulk 'C:\Program Files\Microsoft SQL Server\MSSQL12.SQLEXPRESS\MSSQL\DATA\NYairData.xml', single_blob) as data(d) 

declare @hdoc int 

exec sp_xml_preparedocument @hdoc output, @xml 

select * 
from openxml (@hdoc,'response/row/row',1) 
with (
    _id varchar(100), 
    indicator_data_id int, 
    indicator_id int, 
    measure varchar(1000), 
    geo_type_name varchar(200), 
    geo_entity_id int, 
    geo_entity_name varchar(100), 
    year_description int, 
    data_valuemessage float) 

exec sp_xml_removedocument @hdoc 

這裏一些XML數據我想使用的:

<response> 
    <row> 
    <row _id="1" _uuid="FDE5AC30-B86A-47C5-9A82-9333398F7898" _position="1" _address="http://data.cityofnewyork.us/resource/c3uy-2p5r/1"> 
     <indicator_data_id>130728</indicator_data_id> 
     <indicator_id>646</indicator_id> 
     <name> 
      Air Toxics Concentrations- Average Benzene Concentrations 
     </name> 
     <measure>Average Concentration</measure> 
     <geo_type_name>Borough</geo_type_name> 
     <geo_entity_id>1</geo_entity_id> 
     <geo_entity_name>Bronx</geo_entity_name> 
     <year_description>2005</year_description> 
     <data_valuemessage>2.8</data_valuemessage> 
    </row> 

的數據是從紐約開放數據的網站。這是一個鏈接到源網站:https://data.cityofnewyork.us/Environment/Air-Quality/c3uy-2p5r。我是將XML數據引入DBMS的新手。以下是輸出的屏幕截圖: enter image description here

回答

1

閱讀OPEN XML文檔並查看其中的示例。

另一個好讀:XML Elements vs. Attributes

select * 
from openxml (@hdoc,'response/row/row',2) -- 2 = Use the element-centric mapping. 
with (
    _id varchar(100) './@_id', -- ColPattern to map _id attribute 
    indicator_data_id int, 
    indicator_id int, 
    measure varchar(1000), 
    geo_type_name varchar(200), 
    geo_entity_id int, 
    geo_entity_name varchar(100), 
    year_description int, 
    data_valuemessage float) 
+0

是的,這確實是非常感謝你! –