I'm trying to pass in a value to xml.nodes so that I can genericize:
DECLARE @xml xml =
'
<root>
<level1>
<sublevel1>
<val>this is a value</val>
<val>this is also a value</val>
</sublevel1>
</level1>
</root>
'
declare @xmlPath nvarchar(250) = '/root/level1/sublevel1'
Select @xmlPath as [path], t.c.query('.') as data
from @xml.nodes ('/root/level1/sublevel1') as t(c) --this line find the nodes
--from @xml.nodes ('*[local-name()=sql:variable("@xmlPath")]') as t(c) --This one doesn't
I got to this possible solution when I tried to pass in a variable .Nodes(@xmlPath) or concatenate the path and the Nodes() function required a string literal.
<sublevel1>
<val>this is a value</val>
<val>this is also a value</val>
</sublevel1>
I'm getting no error, but also no data returned when I run this.
References for this method:
https://codegumbo.com/index.php/2013/10/04/sql-server-xquery-functions-sqlvariable-sqlcolumn/
Get SQL xml attribute value using variable

@xmlPath1@xmlPath2@xmlPath3or do you need to have aribitrary depth?