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 "create type"). So I think I can use xml as a variable to pass into the procedure as parameters.
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 ;
I want to use table like @tv as parameter, pass it to procedure, and get the result like this:
<table>
<tr><td>1</td><td>tom</td><td>finance</td></tr>
<tr><td>2</td><td>mark</td><td>business</td></tr>
</table>
I know it can be done by this:
select id as td ,username as td , department as td from @tv for xml raw('tr'),elements
but I want it dynamic, because I don't know the column name when it pass as parameters.
UPDATE (taken from OP's comment)
further more ,I want get the table column name in the result html ,like
<table> <tr> <td>id</td> <td>username</td> <td>department</td> </tr> <tr> <td>1</td> <td>tom</td> <td>finance</td> </tr> <tr> <td>2</td> <td>mark</td> <td>business</td> </tr> </table>
,so when I pass a table variable(or xml ) to function ,it return like this