Generate XML format from SELECT in SQL Server

Generate XML from the SELECT query in SQL Server in different formats as:


Query 1:
select LoginID,JobTitle from 
[AdventureWorks2017].[HumanResources].[Employee]
FOR XML AUTO;

Output 1:
<AdventureWorks2017.HumanResources.Employee LoginID="adventure-works\ken0" JobTitle="Chief Executive Officer" />
<AdventureWorks2017.HumanResources.Employee LoginID="adventure-works\terri0" JobTitle="Vice President of Engineering" />

Query 2:
select LoginID,JobTitle from
[AdventureWorks2017].[HumanResources].[Employee]
FOR XML RAW

Output 2:
<row LoginID="adventure-works\ken0" JobTitle="Chief Executive Officer" />
<row LoginID="adventure-works\terri0" JobTitle="Vice President of Engineering" />

Query 3:
select LoginID,JobTitle from
[AdventureWorks2017].[HumanResources].[Employee]
FOR XML PATH

Output 3:
<row>
  <LoginID>adventure-works\ken0</LoginID>
  <JobTitle>Chief Executive Officer</JobTitle>
</row>
<row>
  <LoginID>adventure-works\terri0</LoginID>
  <JobTitle>Vice President of Engineering</JobTitle>
</row>

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.