我正在尝试返回在sql中具有特定属性的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>
我用来选择包含colorscheme属性的字段的查询是:
SELECT * FROM CustomSettings WITH (NOLOCK)WHERE settings_xml.value('(/Settings/Item/@Name)[1]', 'varchar(25)') = 'ColorScheme' order by 1 desc
我遇到的问题是它只选择将ColorScheme作为第一个字段的xml。如果它进一步位于xml(这是一种可能性),它不会被选中。我理解这是因为我在查询中有[1],但如果删除此部分,则查询不正确。那么我应该把它放在什么地方让它不具体位置?
答案 0 :(得分: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 :(得分: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