SQL Server | Convert Tables in T-SQL into XML
Last Updated :
30 Sep, 2024
XML (Extensible Markup Language) is a widely-used markup language designed to store and transfer structured data between different systems and platforms. While HTML focuses on the visual representation of data
Overview
- XML is similar to HTML which is designed to structure and store data for sharing across different systems and platforms.
- Unlike HTML, XML focuses on the content of the data rather than its display.
- In SQL Server, we can easily convert tables into XML format to facilitate data transfer and integration between applications.
Let’s go through an example of how to achieve this using SQL Server.
Example of XML Document
Here is a simple XML document structure:
<email>
<to>Manager</to>
<from>Sruti</from>
<heading>Work Status</heading>
<body>Work Completed</body>
</email>
Converting Tables in T-SQL to XML
To convert tables from T-SQL to XML in SQL Server, follow these steps. We will first create a table named Employee_Table
to store employee data, and then use SQL queries to generate XML documents from it.
Step 1: Creating the Employee_Table
To create a table to store employee details:
CREATE TABLE Employee_Table
(
EmpId INT IDENTITY(1,1) PRIMARY KEY,
Name VARCHAR(100),
Salary INT,
City VARCHAR(20)
);
Step 2: Inserting Data into Employee_Table
Once the table is created, insert employee data into it:
INSERT INTO Employee_Table (Name, City, Salary)
VALUES
('Sruti', 'Dhanbad', 20000),
('Raj', 'Kerala', 25000),
('Rajsekar', 'Jaipur', 50000),
('Prafull', 'Kochi', 250000),
('Tripti', 'Kolkata', 10000),
('Aditya', 'Mumbai', 5000),
('Kiran', 'Indore', 21000);
Step 3: Verifying the Data
Use the following query to verify the data entered in the table:
SELECT * FROM Employee_Table;
Output:

Methods of Converting Tables in T-SQL to XML
There are two common ways to convert SQL Server tables to XML format:
1. Using FOR XML AUTO
The FOR XML AUTO
clause generates an XML document where each column from the SQL table is represented as an attribute within an element.
SELECT * FROM Employee_Table
FOR XML AUTO;
Output:

This query will create a hyperlink as an output. On clicking the link, we will see the following document in a new query window of SSMS as follows.

2. Using FOR XML PATH
The FOR XML PATH
clause creates an XML document where each row is enclosed in <row>
tags, and each column is embedded in its own XML tag.
SELECT * FROM Employee_Table
FOR XML PATH;
Output:
Conclusion
Converting T-SQL tables into XML format in SQL Server is a valuable technique for sharing structured data across different systems. By using SQL Server’s FOR XML AUTO
and FOR XML PATH
clauses, you can easily transform your table data into a versatile XML document.
Similar Reads
SQL Query to Convert Rows to Columns in SQL Server
In this article we will see, how to convert Rows to Column in SQL Server. In a table where many columns have the have same data for many entries in the table, it is advisable to convert the rows to column. This will help to reduce the table and make the table more readable. For example, Suppose we h
2 min read
CREATE TABLE in SQL Server
SQL Server provides a variety of data management tools such as querying, indexing, and transaction processing. It supports multiple programming languages and platforms, making it a versatile RDBMS for various applications. With its robust features and reliability, SQL Server is a popular choice for
4 min read
BULK INSERT in SQL Server(T-SQL command)
BULK INSERT in SQL Server(T-SQL command): In this article, we will cover bulk insert data from csv file using the T-SQL command in the SQL server and the way it is more useful and more convenient to perform such kind of operations. Let's discuss it one by one. ConditionSometimes there is a scenario
3 min read
SQL Server TRY CONVERT() Function
When we deal with databases, we come across different data types. In SQL we have various data types to store different types of data. Like int data type for integers, varchar data type for strings, date data type for storing the data, and XML for XML type data. For such types of data conversions, we
6 min read
Combine Rows into String in SQL Server
To combine rows into a string in SQL Server, use the SQL COALESCE() function or the SQL CONCAT() function. COALESCE() function in SQL is used to handle null values. It returns non-null values from a row, which can be concatenated into string. CONCAT() function in SQL is used to concatenate two or mo
2 min read
Select into and temporary tables in MS SQL Server
In SQL Server, the SELECT INTO TEMP TABLE statement is used to select data from one or more source tables and insert it into a temporary table. Temporary tables are extremely useful when dealing with intermediate results, or when working with subsets of data within a session without modifying or aff
4 min read
SQL Server Common Table Expressions
SQL Server is a relational database management system (RDBMS) that is used to handle complex data and maintain it in of tabular manner. With the help of SQL Server, one can easily protect their data as it provides various security features. In this article, we are going to explore SQL server's CTE a
8 min read
How to Convert DateTime to VarChar in SQL Server
In SQL Server, date and time values are often stored in the DATETIME or DATE data types. However, there are situations where we may want to convert these values into a different format such as VARCHAR to display the date in a specific format or for further manipulation. In this article, we will expl
4 min read
PL/SQL SELECT INTO Existing Table
PL/SQL is a programming language that is used alongside SQL for writing procedural code such as stored procedures, functions, triggers, and packages within the Oracle Database. It was developed by Oracle Corporation and is widely used in database programming. PL/SQL is a programming language that ha
5 min read
Export SQL Server Data From Table to CSV File
SQL Server is a very popular relational database because of its versatility in exporting data in Excel, CSV, and JSON formats. This feature helps with the portability of data across multiple databases. Here, we will learn how to export SQL Server Data from a table to a CSV file. Tools like Azure Dat
3 min read