6

I am try to implement the following code.

declare @para varchar(10) = 'b';
declare @x xml = '
<x>
    <a>1111</a>
    <b>2222</b>
    <c>3333</c>
</x>';
select @x.query('/x/sql:variable("@para")');

The above code should get the node of <b>2222</b>. However, it get the following error

Msg 9335, Level 16, State 1, Line 8
XQuery [query()]: The XQuery syntax '/function()' is not supported.
ca9163d9
  • 27,283
  • 64
  • 210
  • 413
  • Not familial with XQuery in sql-server, but couldn't help wondering: wouldn't `query(concat('/x/', @para))` work? – grtjn Mar 13 '12 at 20:35
  • @grtjn, the right syntax for query in SQL server is `@x.query('concat("/x/", sql:variable("@para"))')`. However it will return the string of `/x/b`only. – ca9163d9 Mar 14 '12 at 04:29
  • Thnx, learned something new.. – grtjn Mar 14 '12 at 06:57

1 Answers1

12
declare @para varchar(10) = 'b';
declare @x xml = '
<x>
    <a>1111</a>
    <b>2222</b>
    <c>3333</c>
</x>';
select @x.query('/x/*[local-name()=sql:variable("@para")]');
Mikael Eriksson
  • 136,425
  • 22
  • 210
  • 281