Click here to Skip to main content
15,885,065 members
Articles / Database Development / SQL Server / SQL Server 2016

FOR XML Basics (RAW Mode): 1 of 4

Rate me:
Please Sign up or sign in to vote.
4.94/5 (12 votes)
2 Jun 2017CPOL3 min read 16K   11   6
A brief introduction on how to use FOR XML clause in RAW mode in MS SQL Server to return data in XML format

Introduction

A while ago, I was given a task to generate an XML file from database using stored procedure and was given a stored procedure as reference. The reference stored procedure fetched data into a cursor and then processed each record and created XML elements by appending string literals and then returned the generated string "XML". After looking at that stored procedure, I thought to myself that there must be a better way to do this and there was. Using FOR XML clause, one can return table records as XML data. After learning about it, I decided to write an article about it.

Article Series Roadmap

This is article 1 of a 4 part series. Other articles are listed below:

Contents

Background

Basic understanding of SQL Joins is required to follow along.

Using the Code

The article below will use the following database. You can copy/paste the following code to follow along or create your own database and tweak the queries.

Create database as shown below:

SQL
CREATE DATABASE FOR_XML_TUTORIAL;

Execute the below statements to create CUSTOMER and ORDER tables and populate it with data.

SQL
USE [FOR_XML_TUTORIAL];

CREATE TABLE [CUSTOMER]
(
    CUSTOMER_ID INT PRIMARY KEY NOT NULL,
    FIRST_NAME VARCHAR(25) NOT NULL,
    LAST_NAME VARCHAR(25) NOT NULL,
    POSTAL_CODE VARCHAR(2) NOT NULL,
);

CREATE TABLE [ORDER]
(
    ORDER_ID INT PRIMARY KEY NOT NULL,
    CUSTOMER_ID INT NOT NULL REFERENCES CUSTOMER(CUSTOMER_ID),
    TOTAL_ITEMS INT NOT NULL,
    TOTAL_AMOUNT NUMERIC(18,2) NOT NULL
);

INSERT INTO CUSTOMER VALUES (1, 'John', 'Michaels', 'TX');

INSERT INTO CUSTOMER VALUES (2, 'Shawn', 'Cena', 'MA');

INSERT INTO CUSTOMER VALUES (3, 'Dwayne', 'Austin', 'TX');

INSERT INTO CUSTOMER VALUES (4, 'Steve', 'Johnson', 'FL');

INSERT INTO [ORDER] VALUES (1, 1, 5, 32.50);

INSERT INTO [ORDER] VALUES (2, 1, 2, 21.36);

INSERT INTO [ORDER] VALUES (3, 2, 7, 59.00);

INSERT INTO [ORDER] VALUES (4, 3, 2, 18.24);

INSERT INTO [ORDER] VALUES (5, 4, 3, 30.00);

INSERT INTO [ORDER] VALUES (6, 4, 6, 66.00);

FOR XML Modes

When using FOR XML clause, a mode must be specified which returns XML accordingly. Following is a list of available modes:

  • RAW
  • AUTO
  • EXPLICIT
  • PATH

RAW Mode

We will be using the below query to return customer information along with orders.

SQL
SELECT        C.FIRST_NAME,
              C.LAST_NAME,
              C.POSTAL_CODE,
              O.ORDER_ID,
              O.TOTAL_ITEMS,
              O.TOTAL_AMOUNT
FROM          [CUSTOMER] C
INNER JOIN    [ORDER] O
ON            C.CUSTOMER_ID = O.CUSTOMER_ID

The query returns.

query data

To return XML data in RAW mode, simply append FOR XML RAW in the above query.

SQL
SELECT        C.FIRST_NAME,
              C.LAST_NAME,
              C.POSTAL_CODE,
              O.ORDER_ID,
              O.TOTAL_ITEMS,
              O.TOTAL_AMOUNT
FROM          [CUSTOMER] C
INNER JOIN    [ORDER] O
ON            C.CUSTOMER_ID = O.CUSTOMER_ID
FOR XML RAW

The above query returns:

Image 2

As you can see, each record returned by the query was converted to an XML element "row" and columns into attributes with attribute values set to values returned by query.

To change the element name, append the desired element name after RAW keyword as below:

SQL
SELECT        C.FIRST_NAME,
              C.LAST_NAME,
              C.POSTAL_CODE,
              O.ORDER_ID,
              O.TOTAL_ITEMS,
              O.TOTAL_AMOUNT
FROM          [CUSTOMER] C
INNER JOIN    [ORDER] O
ON            C.CUSTOMER_ID = O.CUSTOMER_ID
FOR XML RAW('Customer')

The above query returns:

Image 3

This changed each element's name from row to Customer.

To change attribute's name, simply provide alias to columns as below:

SQL
SELECT        C.FIRST_NAME AS 'FirstName',
              C.LAST_NAME AS 'LastName',
              C.POSTAL_CODE AS 'PostalCode',
              O.ORDER_ID AS 'OrderId',
              O.TOTAL_ITEMS AS 'Items',
              O.TOTAL_AMOUNT AS 'Amount'
FROM          [CUSTOMER] C
INNER JOIN    [ORDER] O
ON            C.CUSTOMER_ID = O.CUSTOMER_ID
FOR XML RAW('Customer')

The above query returns data with new attribute names:

Image 4

To nest each element inside a root element, append ROOT keyword as below:

SQL
SELECT        C.FIRST_NAME AS 'FirstName',
              C.LAST_NAME AS 'LastName',
              C.POSTAL_CODE AS 'PostalCode',
              O.ORDER_ID AS 'OrderId',
              O.TOTAL_ITEMS AS 'Items',
              O.TOTAL_AMOUNT AS 'Amount'
FROM          [CUSTOMER] C
INNER JOIN    [ORDER] O
ON            C.CUSTOMER_ID = O.CUSTOMER_ID
FOR XML RAW('Customer'), ROOT

The data returned is now nested inside a parent element named "root".

Image 5

To change root element's name, append the desired name after ROOT keyword as below:

SQL
SELECT        C.FIRST_NAME AS 'FirstName',
              C.LAST_NAME AS 'LastName',
              C.POSTAL_CODE AS 'PostalCode',
              O.ORDER_ID AS 'OrderId',
              O.TOTAL_ITEMS AS 'Items',
              O.TOTAL_AMOUNT AS 'Amount'
FROM          [CUSTOMER] C
INNER JOIN    [ORDER] O
ON            C.CUSTOMER_ID = O.CUSTOMER_ID
FOR XML RAW('Customer'), ROOT('Customers')

The above query returns data with root element's name changed to "Customers".

Image 6

Till now, all the queries we executed returned XML data in a format in which each column was converted to an attribute. To change this format and return XML in which each column is mapped to its own element, append ELEMENTS keyword as below:

SQL
SELECT        C.FIRST_NAME AS 'FirstName',
              C.LAST_NAME AS 'LastName',
              C.POSTAL_CODE AS 'PostalCode',
              O.ORDER_ID AS 'OrderId',
              O.TOTAL_ITEMS AS 'Items',
              O.TOTAL_AMOUNT AS 'Amount'
FROM          [CUSTOMER] C
INNER JOIN    [ORDER] O
ON            C.CUSTOMER_ID = O.CUSTOMER_ID
FOR XML RAW('Customer'), ROOT('Customers'), ELEMENTS

Now each record's attribute is converted into an element with its name set to its alias and value set to the value returned by query.

Note: I reduced the number of rows to make the below image smaller:

Image 7

Wrapping Up

That is all for RAW mode in FOR XML clause. In the next article, we will learn about AUTO mode.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Pakistan Pakistan
Software Engineer and a clean code proponent, working on various .NET, Oracle and front end technologies including Web Forms, MVC, Web API, LINQ, EF, T-SQL, PL/SQL, SSIS, JavaScript, jQuery.

Comments and Discussions

 
QuestionPart 4 of 4 ? Pin
Member 1089380629-Jan-20 9:03
Member 1089380629-Jan-20 9:03 
GeneralMy vote of 5 Pin
Ehsan Sajjad10-Jun-17 9:39
professionalEhsan Sajjad10-Jun-17 9:39 
QuestionMy vote of 5 Pin
Member 1647401-Jun-17 4:49
Member 1647401-Jun-17 4:49 
GeneralMy vote of 5 Pin
Member 1236439031-May-17 21:15
Member 1236439031-May-17 21:15 
SuggestionMSDN Reference Pin
martinrj3031-May-17 16:03
martinrj3031-May-17 16:03 
Questiontried adding custom assemblies? Pin
ZerqTheMad30-May-17 22:04
ZerqTheMad30-May-17 22:04 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.