title: T-SQL retrieving sql attribute from xml using variable
body: <p>I am trying to retrieve an XML attribute from an XML variable passing in the name of the desired attribute. The first select statement works just fine retrieving the correct attribute values. However, when I try to set within a SQL variable the desired attribute name, all that is displayed is the string <code>/root/attribs/@id</code> instead of the actual value. I have tried numerous permutations of the <code>@path</code> variable, all to no avail.  </p>
  
  <p>What am I missing here?</p>
  
  <pre><code>DECLARE @XMLString XML = '&lt;root&gt;&lt;attribs flags="1" id="test_id" platform="test_platform" /&gt;&lt;/root&gt;';
  
  SELECT
      flags = x.c.value('(/root/attribs/@flags)[1]', 'nvarchar(50)') ,
      id = x.c.value('(/root/attribs/@id)[1]', 'nvarchar(50)') ,
      [platform] = x.c.value('(/root/attribs/@platform)[1]', 'nvarchar(50)')
  FROM
      @XMLString.nodes('/*') x ( c );
  
  DECLARE @Path NVARCHAR(50) = '/root/attribs/@id';
  SELECT
      result = x.c.value('(sql:variable("@Path"))[1]', 'nvarchar(50)')
  FROM
      @XMLString.nodes('/*') x ( c );
  </code></pre>