2013-01-21 124 views
0
<storage xmlns="http://energy" created-on="2013-01-21T05:00:15Z"> 
<country>Italy</country> 
<Points> 
    <point>Callato;Cellino</point> 
</Points> 
    </storage> 

上面的數據是表中的列(xmlcolumn),用於從上面檢索單獨創建的列。當我使用下面的查詢我得到空從xml列中獲取屬性值

SELECT 
OutputXML.value('(storage/@created-on)[1]','date') 
    AS ProductType,* 
FROM [DataOutput]; 

什麼錯誤我在做什麼,感謝阿倫

回答

0

你需要指定的命名空間爲您的查詢:

declare @t table (a xml) 
insert into @t(a) values ('<storage xmlns="http://energy" created-on="2013-01-21T05:00:15Z"> 
<country>Italy</country> 
<Points> 
    <point>Callato;Cellino</point> 
</Points> 
    </storage>') 

SELECT 
a.value('declare namespace x = "http://energy"; 
      (x:storage/@created-on)[1]','date') 
    AS ProductType,* 
FROM @t; 

我爲了便於閱讀,我們將它分成兩行。然而,沒有必要這樣做。

結果:

ProductType a 
----------- ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 
2013-01-21 <storage xmlns="http://energy" created-on="2013-01-21T05:00:15Z"><country>Italy</country><Points><point>Callato;Cellino</point></Points></storage> 
+0

由於其工作@damien –