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
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
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
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
Table operations in MS SQL Server
In a relational database, the data is stored in the form of tables and each table is referred to as a relation. A table can store a maximum of 1000 rows. Tables are a preferred choice as: Tables are arranged in an organized manner. We can segregate the data according to our preferences in the form o
2 min read
SQL INSERT INTO SELECT Statement
In SQL, the INSERT INTO statement is used to add or insert records into the specified table. We use this statement to insert data directly into a table by specifying column names in a specific order. The SELECT statement is used to retrieve data from the table, and it can be used in conjunction with
6 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
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
Insert Statement in MS SQL Server
The SQL Server INSERT statement is a fundamental command used to add new rows of data to a table. Whether we are inserting specific values, utilizing default values or copying data from another table. In this guide, weâll explore various ways to use the Insert statement in MS SQL Server with the hel
4 min read