title: XQUERY - How to use the sql:variable in 'value()' function?
body: <p>The query below is trying to select a child node of a given Node. How do I use a variable instead of hard coding the child node such that I can pass them as parameters in a SProc?</p>
  
  <pre><code>declare @T table(XMLCol xml)
  insert into @T values
  ('&lt;Root xmlns="http://tempuri.org" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"&gt;
    &lt;Elem1 type="T1"&gt;
      &lt;Name type="string" display="First name"&gt;John&lt;/Name&gt;
      &lt;TimeZone display="Time zone"&gt;
        &lt;children&gt;
        &lt;DisplayName type="string" display="Display name"&gt;GMT Standard Time&lt;/DisplayName&gt;
        &lt;HiddenName type="string" display="Hidden name"&gt;GMT&lt;/HiddenName&gt;
        &lt;/children&gt;
      &lt;/TimeZone&gt;
    &lt;/Elem1&gt;
  &lt;/Root&gt;') 
  
  declare @Node varchar(50)
  set @Node = 'TimeZone'
  
  select N.value('(children/DisplayName)[1]', 'varchar(100)') as Value
  from @T as T
    cross apply T.XMLCol.nodes('//*[local-name()=sql:variable("@Node")]') as X(N)
  </code></pre>