Header add

In this article we will learn about How to convert table data into xml in sql server. we will focus on how tables will be converted in T-SQL into XML in SQL server. And you will be able to understand how you can convert it with the help of command.

We can convert the data table into the XML format using  
  • XML Raw 
  • XML Path
  • XML Auto 
Here I am going to use the XML Raw to convert the data table to XML format


XML Raw

RAW mode transform each row in the query result into an XML element. Below is the customer table structure.

Customer Table

The query to convert data table to XML format like below;

select top 10 * from customers for xml raw('customer'),root('CustomerDetails') 

The output of XML like below. Here we select top 10 record from customer table.

XML


XML Path

select top 2 * from customers for xml path('customer'),root('CustomerDetails')

XML Path




XML Auto

select top 2 * from customers for xml auto,root('CustomerDetails')

XML Auto


Summary

So far we discussed How to convert table data into xml in sql server. Let us know if you have any question.

Post a Comment

Previous Post Next Post