Note that there are some explanatory texts on larger screens.

plurals
  1. POSQL XML query assistance
    primarykey
    data
    text
    <p>I've got a table in a SQL Server 2008 database with an nvarchar(MAX) column containing XML data. The data represents search criteria. Here's what the XML looks like for search criteria with one top-level "OR" group containing one single criterion and a nested two-criterion "AND" group.</p> <pre><code>&lt;?xml version="1.0" encoding="utf-16"?&gt; &lt;SearchCriterionGroupArgs xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"&gt; &lt;SingleCriteria&gt; &lt;SearchCriterionSingleArgs&gt; &lt;Operator&gt;Equals&lt;/Operator&gt; &lt;Value&gt;test&lt;/Value&gt; &lt;FieldIDs&gt; &lt;int&gt;1026&lt;/int&gt; &lt;int&gt;478&lt;/int&gt; &lt;/FieldIDs&gt; &lt;EntityID&gt;92&lt;/EntityID&gt; &lt;/SearchCriterionSingleArgs&gt; &lt;/SingleCriteria&gt; &lt;GroupCriteria&gt; &lt;SearchCriterionGroupArgs&gt; &lt;SingleCriteria&gt; &lt;SearchCriterionSingleArgs&gt; &lt;Operator&gt;GreaterThan&lt;/Operator&gt; &lt;Value&gt;2010-01-23&lt;/Value&gt; &lt;FieldIDs&gt; &lt;int&gt;1017&lt;/int&gt; &lt;/FieldIDs&gt; &lt;EntityID&gt;92&lt;/EntityID&gt; &lt;/SearchCriterionSingleArgs&gt; &lt;SearchCriterionSingleArgs&gt; &lt;Operator&gt;LessThan&lt;/Operator&gt; &lt;Value&gt;2013-01-23&lt;/Value&gt; &lt;FieldIDs&gt; &lt;int&gt;1018&lt;/int&gt; &lt;/FieldIDs&gt; &lt;EntityID&gt;92&lt;/EntityID&gt; &lt;/SearchCriterionSingleArgs&gt; &lt;/SingleCriteria&gt; &lt;GroupCriteria /&gt; &lt;EntityID&gt;92&lt;/EntityID&gt; &lt;LogicalOperator&gt;AND&lt;/LogicalOperator&gt; &lt;/SearchCriterionGroupArgs&gt; &lt;/GroupCriteria&gt; &lt;EntityID&gt;92&lt;/EntityID&gt; &lt;LogicalOperator&gt;OR&lt;/LogicalOperator&gt; &lt;/SearchCriterionGroupArgs&gt; </code></pre> <p>Given a an input set of FieldID values, I need to search the table to find if there are any records whose search criteria refer to one of those values (these are represented in the "int" nodes under the "FieldIDs" nodes.)</p> <p>By running this query:</p> <pre><code>select CAST(OptionalConditions as xml).query('//FieldIDs') from tblMyTable </code></pre> <p>I get the results:</p> <pre><code>&lt;FieldIDs&gt; &lt;int&gt;1026&lt;/int&gt; &lt;int&gt;478&lt;/int&gt; &lt;/FieldIDs&gt; &lt;FieldIDs&gt; &lt;int&gt;1017&lt;/int&gt; &lt;/FieldIDs&gt; &lt;FieldIDs&gt; &lt;int&gt;1018&lt;/int&gt; &lt;/FieldIDs&gt; </code></pre> <p>(currently there's only one record in the table with xml data in it.)</p> <p>But I'm just getting started with this stuff and I don't know what the notation would be to check those lists for the existence of any of an arbitrary set of FieldIDs. I don't need to retrieve any particular nodes, just true or false for whether the input field IDs are referenced anywhere in the search.</p> <p>Thanks for your help!</p> <p>Edit: using Ranon's solution, I got it working using a query like this:</p> <pre><code>SELECT * FROM myTable WHERE CAST(OptionalConditions as xml).exist('//FieldIDs/int[.=(1019,111,1018)]') = 1 </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload