title: SQL - Using a variable as the entire XQuery path in XML.value()
body: <p>Is it possible to use a variable to define the entire XQuery path when using <code>.value()</code> on an XML field?</p>
  
  <p>Using <code>[local-name()=sql:variable("@FilterA")]</code>, I can define the <em>filter</em> I want to apply as a pair of variables, but I can't get a valid syntax for the entire XQuery path to work.</p>
  
  <p><strong>Example</strong></p>
  
  <pre><code>DECLARE @myData AS TABLE (myID INT, Parameter XML)
  INSERT INTO @myData VALUES
  (1, '&lt;paramdata&gt;&lt;Date&gt;19/06/15&lt;/Date&gt;&lt;term&gt;1&lt;/term&gt;&lt;/paramdata&gt;'),
  (2, '&lt;paramdata&gt;&lt;Date&gt;19/06/15&lt;/Date&gt;&lt;term&gt;5&lt;/term&gt;&lt;/paramdata&gt;')
  
  --This works as expected
  SELECT * FROM @myData
  WHERE Parameter.value('/paramdata/term=5','bit') = 1
  
  
  --Two variables is possible
  DECLARE @FilterA VARCHAR(255) = 'term'
  DECLARE @FilterB VARCHAR(255) = '5'
  
  SELECT * FROM @myData
  WHERE Parameter.value('(/paramdata/*[local-name()=sql:variable("@FilterA")])[1]','int') = @FilterB
  
  --but a single variable isn't
  DECLARE @Filter1 VARCHAR(255) = '/paramdata/term=5'
  SELECT * FROM @myData
  WHERE Parameter.value('[local-name()=sql:variable("@Filter1")]','bit') = 1
  </code></pre>
  
  <p>I'm aware that casting the whole query to a string and "EXEC"-ing it should work, but this likely not suitable for the larger case where I want to apply this.</p>
  
  <p>EDIT</p>
  
  <p>After reading about the XY problem this question should be rephrased as:<br>
  <em>"is there a way to use an argument of the format <code>SomeNode=SomeValue</code> to filter on XML columns?"</em>  </p>