2011-03-07 58 views
4

特別是,如果在一個名爲元數據的列中給出以下XML內容,如何檢索具有特定名稱屬性的節點的值?SQL 2008從XML列中檢索屬性值

在這種情況下與int16_t標籤和 'filterIndex'

<root version="1"> 
    <class derived_type="FilterInfo" name="FilterInfo"> 
    <string name="filterName" length="3" value="mt8" /> 
    <string name="filterText" length="3" value="SVR" /> 
    <string name="filterTable" length="11" value="CML_5_1_mt8" /> 
    <bool name="userDefined" value="true" /> 
    <bool name="multiResp" value="false" /> 
    <enumeration name="filterType" value="1" /> 
    <enumeration name="filterCategory" value="1" /> 
    <vector name="options" count="1"> 
     <class derived_type="OptionInfo" name="V0"> 
     <int16_t name="optionIndex" value="1" /> 
     <string name="optionName" length="3" value="mt8" /> 
     <string name="optionText" length="3" value="SVR" /> 
     <string name="optionTable" length="11" value="CML_5_1_mt8" /> 
     </class> 
    </vector> 
    <GUID name="primaryKey" value="fe66b60f-468a-4f0e-be80-0055e20baa35" /> 
    <int16_t name="filterIndex" value="1080" /> 
    </class> 
</root> 

THX ++

傑里名字關聯的值之後我。

回答

5

這樣的事情?

SELECT 
    metadata.value('(/root/class/int16_t/@value)[1]', 'int') AS 'int16_value' 
FROM 
    dbo.YourTable 
+0

靶心。 THX ++。 – 2011-03-07 16:55:41

+0

這很方便,因爲在查詢路徑中只有一個標記名爲「int16_t」的元素。如果你想要'name'屬性== filterName的'string'元素呢? – 2012-12-03 11:25:23