Is it possible to use a variable to define the entire XQuery path when using .value() on an XML field?
Using [local-name()=sql:variable("@FilterA")], I can define the filter I want to apply as a pair of variables, but I can't get a valid syntax for the entire XQuery path to work.
Example
DECLARE @myData AS TABLE (myID INT, Parameter XML)
INSERT INTO @myData VALUES
(1, '<paramdata><Date>19/06/15</Date><term>1</term></paramdata>'),
(2, '<paramdata><Date>19/06/15</Date><term>5</term></paramdata>')
--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
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.
EDIT
After reading about the XY problem this question should be rephrased as:
"is there a way to use an argument of the format SomeNode=SomeValue to filter on XML columns?"
paramdatawhy would you want to pass in the filter as/paramdata/SomeNode=SomeValue? Was it possible to pass inSomeNode=SomeValue?