Edwin Aguilar
Edwin Aguilar

Reputation: 11

Function to create xml with column names as elements

I need to write a basic t-sql function f that receives the following parameters:

(Id int, CustomerId int, Delay int)

and creates, for f(1, 125, 32), the following xml, to be later inserted into a xml column in a table:

<content>   
<column name="Id">1</column>   
<column name="CustomerId">125</column>   
<column name="Delay">125</column>   
</content>

Any ideas on how to attack the problem?

Upvotes: 1

Views: 98

Answers (1)

roman
roman

Reputation: 117510

select *
from (values
    (@Id, 'Id'), (@CustomerId, 'CustomerId'), (@Delay, 'Delay')
) as a([@Name], [text()])
for xml path('column'), root('content')

sql fiddle demo

Upvotes: 1

Related Questions