SQL SERVER: Convert Table Data to XML Format using FOR XML PATH()

Introduction: In this article I am going to explain how to convert sql table data to different xml format using FOR XML PATH().  


Implementation: Let’s create a temporary table for demonstration purpose and insert some dummy data into it using the following script.


CREATE TABLE #EmpTable
(
Id         INT,
Name  VARCHAR(100),
Age     INT
)

INSERT INTO #EmpTable
VALUES
(1, 'Aamir',24),
(2, 'Ranbeer',32),
(3, 'Alisha',23),
(4, 'Aalia',21),
(5, 'Varun',28)

--Check data
Select * from #EmpTable

Result:
Id
Name
Age
1
Aamir
24
2
Ranbeer
32
3
Alisha
23
4
Aalia
21
5
Varun
28

Now suppose we want to convert sql table data into the format given below:

<Root>
  <Employee>
    <Id>1</Id>
    <Name>Aamir</Name>
    <Age>24</Age>
  </Employee>
  <Employee>
    <Id>2</Id>
    <Name>Ranbeer</Name>
    <Age>32</Age>
  </Employee>
  <Employee>
    <Id>3</Id>
    <Name>Alisha</Name>
    <Age>23</Age>
  </Employee>
  <Employee>
    <Id>4</Id>
    <Name>Aalia</Name>
    <Age>21</Age>
  </Employee>
  <Employee>
    <Id>5</Id>
    <Name>Varun</Name>
    <Age>28</Age>
  </Employee>
</Root>

Then the query will be as simple as:
SELECT Id, Name, Age FROM #EmpTable FOR XML PATH('Employee'), ROOT('Root')


Now suppose we want to convert sql table data into the format given below:

<Root>
  <Employee Id="1">
    <Name>Aamir</Name>
    <Age>24</Age>
  </Employee>
  <Employee Id="2">
    <Name>Ranbeer</Name>
    <Age>32</Age>
  </Employee>
  <Employee Id="3">
    <Name>Alisha</Name>
    <Age>23</Age>
  </Employee>
  <Employee Id="4">
    <Name>Aalia</Name>
    <Age>21</Age>
  </Employee>
  <Employee Id="5">
    <Name>Varun</Name>
    <Age>28</Age>
  </Employee>
</Root>

Then the query will be as simple as:
SELECT Id AS '@Id', Name , Age FROM #EmpTable FOR XML PATH('Employee'), ROOT('Root')

Now suppose we want to convert sql table data into the format given below:

<Root>
  <Employee Id="1" Name="Aamir" Age="24" />
  <Employee Id="2" Name="Ranbeer" Age="32" />
  <Employee Id="3" Name="Alisha" Age="23" />
  <Employee Id="4" Name="Aalia" Age="21" />
  <Employee Id="5" Name="Varun" Age="28" />
</Root>

Then the query will be as simple as:

SELECT Id AS '@Id', Name AS '@Name', Age AS '@Age' FROM #EmpTable FOR XML PATH('Employee'), ROOT('Root')

Now over to you:
A blog is nothing without reader's feedback and comments. So please provide your valuable feedback so that i can make this blog better and If you like my work; you can appreciate by leaving your comments, hitting Facebook like button, following on Google+, Twitter, Linkedin and Pinterest, stumbling my posts on stumble upon and subscribing for receiving free updates directly to your inbox . Stay tuned and stay connected for more technical updates. 
Previous
Next Post »

If you have any question about any post, Feel free to ask.You can simply drop a comment below post or contact via Contact Us form. Your feedback and suggestions will be highly appreciated. Also try to leave comments from your account not from the anonymous account so that i can respond to you easily..