2011-12-22 76 views
0

我想返回在sql中具有特定屬性的xml。在文檔中的任意位置選擇包含xml節點的行

的XML看起來是這樣的:

<Settings> 
    <Item Name="ColorScheme" Type="XDocument"> 
    <ColorScheme> 
    <ItemStyle Name="element-song" Title="Song" ForeColor="#0538ac" BackColor="#e8f1fa" Media="1" /> 
    <ItemStyle Name="element-song-print" Title="Song (print)" ForeColor="#0538ac" BackColor="" Media="2" /> 
    <ItemStyle Name="element-spot" Title="Spot" ForeColor="#048b27" BackColor="#e9ffee" Media="1" /> 
    <ItemStyle Name="element-spot-print" Title="Spot (print)" ForeColor="#048b27" BackColor="" Media="2" /> 
    <ItemStyle Name="element-programming" Title="Programming" ForeColor="#0538ac" BackColor="#e8f1fa" Media="1" /> 
    <ItemStyle Name="element-programming-print" Title="Programming (print)" ForeColor="#0538ac" BackColor="" Media="2" /> 
    <ItemStyle Name="element-continued-programming" Title="Continued programming" ForeColor="#0538ac" BackColor="#e8f1fa" Media="1" /> 
    <ItemStyle Name="element-continued-programming-print" Title="Continued programming (print)" ForeColor="#0538ac" BackColor="" Media="2" /> 
    </ColorScheme> 
    </Item> 
    <Item Name="SendWeeklyNewAccountEmail" Type="Boolean">False</Item> 
    <Item Name="UseEstimatedDurationsForAnalysis" Type="Boolean">True</Item> 
    <Item Name="CalendarStyle" Type="Int32">1</Item> 
    <Item Name="IncludeAdExForAnalysis" Type="Boolean">True</Item> 
</Settings> 

我用它來選擇包含的色彩方案屬性這些字段的查詢是:

SELECT * FROM CustomSettings WITH (NOLOCK)WHERE settings_xml.value('(/Settings/Item/@Name)[1]', 'varchar(25)') = 'ColorScheme' order by 1 desc 

我遇到的問題是,它只是選擇具有ColorScheme作爲第一個字段的xml。如果它位於xml中(這是一種可能性),它不會被選中。我明白這是由於我在查詢中有[1],但如果我刪除了這部分,查詢就不會被糾正。那麼我該如何放置它來使其不具體位置?

回答

1
SELECT * 
FROM CustomSettings 
WITH (NOLOCK) 
WHERE settings_xml.value('(/Settings/Item[@name="ColorScheme"]/@Name)[1]', 'varchar(25)') IS NOT NULL 
order by 1 desc 
1
DECLARE @x TABLE (x XML) 

INSERT INTO @x 
VALUES ('<Settings> <Item Name="ColorScheme" Type="XDocument"> <ColorScheme>  <ItemStyle Name="element-song" Title="Song" ForeColor="#0538ac" BackColor="#e8f1fa" Media="1" />  <ItemStyle Name="element-song-print" Title="Song (print)" ForeColor="#0538ac" BackColor="" Media="2" />  <ItemStyle Name="element-spot" Title="Spot" ForeColor="#048b27" BackColor="#e9ffee" Media="1" />  <ItemStyle Name="element-spot-print" Title="Spot (print)" ForeColor="#048b27" BackColor="" Media="2" />  <ItemStyle Name="element-programming" Title="Programming" ForeColor="#0538ac" BackColor="#e8f1fa" Media="1" />  <ItemStyle Name="element-programming-print" Title="Programming (print)" ForeColor="#0538ac" BackColor="" Media="2" />  <ItemStyle Name="element-continued-programming" Title="Continued programming" ForeColor="#0538ac" BackColor="#e8f1fa" Media="1" />  <ItemStyle Name="element-continued-programming-print" Title="Continued programming (print)" ForeColor="#0538ac" BackColor="" Media="2" /> </ColorScheme> </Item> <Item Name="SendWeeklyNewAccountEmail" Type="Boolean">False</Item> <Item Name="UseEstimatedDurationsForAnalysis" Type="Boolean">True</Item> <Item Name="CalendarStyle" Type="Int32">1</Item> <Item Name="IncludeAdExForAnalysis" Type="Boolean">True</Item> </Settings> ') 
, ('<Settings> <Item Name="SendWeeklyNewAccountEmail" Type="Boolean">False</Item> <Item Name="UseEstimatedDurationsForAnalysis" Type="Boolean">True</Item> <Item Name="CalendarStyle" Type="Int32">1</Item> <Item Name="IncludeAdExForAnalysis" Type="Boolean">True</Item> </Settings>)') 
, ('<Settings> <Item Name="IncludeAdExForAnalysis" Type="Boolean">True</Item> <Item Name="ColorScheme" Type="XDocument"> <ColorScheme>  <ItemStyle Name="element-song" Title="Song" ForeColor="#0538ac" BackColor="#e8f1fa" Media="1" />  <ItemStyle Name="element-song-print" Title="Song (print)" ForeColor="#0538ac" BackColor="" Media="2" />  <ItemStyle Name="element-spot" Title="Spot" ForeColor="#048b27" BackColor="#e9ffee" Media="1" />  <ItemStyle Name="element-spot-print" Title="Spot (print)" ForeColor="#048b27" BackColor="" Media="2" />  <ItemStyle Name="element-programming" Title="Programming" ForeColor="#0538ac" BackColor="#e8f1fa" Media="1" />  <ItemStyle Name="element-programming-print" Title="Programming (print)" ForeColor="#0538ac" BackColor="" Media="2" />  <ItemStyle Name="element-continued-programming" Title="Continued programming" ForeColor="#0538ac" BackColor="#e8f1fa" Media="1" />  <ItemStyle Name="element-continued-programming-print" Title="Continued programming (print)" ForeColor="#0538ac" BackColor="" Media="2" /> </ColorScheme> </Item> <Item Name="SendWeeklyNewAccountEmail" Type="Boolean">False</Item> <Item Name="UseEstimatedDurationsForAnalysis" Type="Boolean">True</Item> <Item Name="CalendarStyle" Type="Int32">1</Item> </Settings> ') 


SELECT * 
FROM @x x1 
WHERE x1.x.exist('(/Settings/Item[@Name="ColorScheme"])') = 1 
相關問題