title: How to convert xml as a table or html table in sqlserver
body: <p>xml is a type in sqlserver, now I want to pass a table variable to procedure, but you must define the table as type (use &quot;create type&quot;).
  So I think I can use xml as a variable to pass into the procedure as parameters.</p>
  <pre><code>declare @tv  table
  (
    id int, 
    username varchar(50),
    department varchar(50) 
    
  )
  insert into @tv values(1,'tom','finance'),(2,'mark','business');
  
  declare @xml xml;
  set @xml  =(select * from @tv for xml  path('row') ,type )
  select @xml  ;
  </code></pre>
  <p>I want to use table like @tv as parameter, pass it to procedure,
  and get the result like this:</p>
  <pre><code>&lt;table&gt;
  &lt;tr&gt;&lt;td&gt;1&lt;/td&gt;&lt;td&gt;tom&lt;/td&gt;&lt;td&gt;finance&lt;/td&gt;&lt;/tr&gt;
  &lt;tr&gt;&lt;td&gt;2&lt;/td&gt;&lt;td&gt;mark&lt;/td&gt;&lt;td&gt;business&lt;/td&gt;&lt;/tr&gt;
  &lt;/table&gt;
  </code></pre>
  <p>I know it can be done by this:</p>
  <p><code>select id as td ,username as td , department as td from @tv for  xml raw('tr'),elements </code></p>
  <p>but I want it dynamic, because I don't know the column name when it pass as parameters.</p>
  <h2>UPDATE (taken from OP's comment)</h2>
  <p>further more ,I want get the table column name in the result html ,like</p>
  <pre><code>&lt;table&gt; &lt;tr&gt; &lt;td&gt;id&lt;/td&gt; &lt;td&gt;username&lt;/td&gt; &lt;td&gt;department&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;1&lt;/td&gt; &lt;td&gt;tom&lt;/td&gt; &lt;td&gt;finance&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;2&lt;/td&gt; &lt;td&gt;mark&lt;/td&gt; &lt;td&gt;business&lt;/td&gt; &lt;/tr&gt; &lt;/table&gt;
  </code></pre>
  <p>,so when I pass a table variable(or xml ) to function ,it return like this</p>