Skip to content

shannonlowder.com

Menu
  • About
  • Biml Interrogator Demo
  • Latest Posts
Menu

SQL 301-FOR XML RAW

Posted on February 9, 2011July 6, 2011 by slowder

OK, I need to study my XML queries, so I’m going to write them up as I go.  That way you can learn along with me.  In a normal query we return the results in either datagrid or text view, right.  Well, if we want to transmit the results to another user or a process, more than likely we’re going to want those results in XML.  By adding FOR XML to the end of our queries we can do that.

But don’t get too far ahead.  There are four basic modes to FOR XML.  You have RAW, AUTO, EXPLICIT, or PATH.  We’re going to start with RAW.

I’m going to work out of the adventureworks database on my SQL 2008 R2 instance.  Consider the following query:

SELECT TOP 5
     ContactID, Title, FirstName, MiddleName, LastName, Suffix
FROM Person.Contact

In datagrid mode you’d get the following results.

But if you add FOR XML RAW to the end your query:

SELECT TOP 5
     ContactID, Title, FirstName, MiddleName, LastName, Suffix
FROM Person.Contact
FOR XML RAW

Then you get something different

A link... Click it! It's not spam, I promise!

When you click the link, you should see something like this:

<row ContactID="1" Title="Mr." FirstName="Gustavo" LastName="Achong" />
<row ContactID="2" Title="Ms." FirstName="Catherine" MiddleName="R." LastName="Abel" />
<row ContactID="3" Title="Ms." FirstName="Kim" LastName="Abercrombie" />
<row ContactID="4" Title="Sr." FirstName="Humberto" LastName="Acevedo" />
<row ContactID="5" Title="Sra." FirstName="Pilar" LastName="Ackerman" />

It’s valid XML, but it’s not an XML document, it’s only an XML Fragment.  There is no root node, each element is called “row”, your columnNames become the attribute names, and the values… well they’re still values.  Notice  the first, third, fourth, and last rows.  They have no Middle name values.  That’s because when you’re in RAW mode, if a value is NULL, then that attribute is skipped in the output.

If we wanted to overcome the lack of a root node, we could do that by adding the ROOT(‘name’) argument to our query like this:

SELECT TOP 5
     ContactID, Title, FirstName, MiddleName, LastName, Suffix
FROM Person.Contact
FOR XML RAW, ROOT('Contacts')
--after clicking the link in SSMS you'd get:
<Contacts>
 <row ContactID="1" Title="Mr." FirstName="Gustavo" LastName="Achong" />
 <row ContactID="2" Title="Ms." FirstName="Catherine" MiddleName="R." LastName="Abel" />
 <row ContactID="3" Title="Ms." FirstName="Kim" LastName="Abercrombie" />
 <row ContactID="4" Title="Sr." FirstName="Humberto" LastName="Acevedo" />
 <row ContactID="5" Title="Sra." FirstName="Pilar" LastName="Ackerman" />
</Contacts>

If you want to take this a step further, and change the name of the elements, the RAW keyword can accept an argument that will change the name of the rows.  Let’s change it to Contact, since each row represents one contact.

SELECT TOP 5
     ContactID, Title, FirstName, MiddleName, LastName, Suffix
FROM Person.Contact
FOR XML RAW('Contact'), ROOT('Contacts')
--again, after clicking the link in SSMS, you see:
<Contacts>
 <Contact ContactID="1" Title="Mr." FirstName="Gustavo" LastName="Achong" />
 <Contact ContactID="2" Title="Ms." FirstName="Catherine" MiddleName="R." LastName="Abel" />
 <Contact ContactID="3" Title="Ms." FirstName="Kim" LastName="Abercrombie" />
 <Contact ContactID="4" Title="Sr." FirstName="Humberto" LastName="Acevedo" />
 <Contact ContactID="5" Title="Sra." FirstName="Pilar" LastName="Ackerman" />
</Contacts>

Now the last step of customization I can find for XML RAW is to change the attributes into elements.  All you have to do is add one more keyword to the query: ELEMENTS.

SELECT TOP 5
     ContactID, Title, FirstName, MiddleName, LastName, Suffix
FROM Person.Contact
FOR XML RAW('Contact'), ROOT('Contacts'), ELEMENTS
--And after clicking the link in SSMS, you see:
<Contacts>
 <Contact>
  <ContactID>1</ContactID>
  <Title>Mr.</Title>
  <FirstName>Gustavo</FirstName>
  <LastName>Achong</LastName>
 </Contact>
 <Contact>
  <ContactID>2</ContactID>
  <Title>Ms.</Title>
  <FirstName>Catherine</FirstName>
  <MiddleName>R.</MiddleName>
  <LastName>Abel</LastName>
 </Contact>
 <Contact>
  <ContactID>3</ContactID>
  <Title>Ms.</Title>
  <FirstName>Kim</FirstName>
  <LastName>Abercrombie</LastName>
 </Contact>
 <Contact>
 <ContactID>4</ContactID>
  <Title>Sr.</Title>
  <FirstName>Humberto</FirstName>
  <LastName>Acevedo</LastName>
 </Contact>
 <Contact>
 <ContactID>5</ContactID>
  <Title>Sra.</Title>
  <FirstName>Pilar</FirstName>
  <LastName>Ackerman</LastName>
 </Contact>
</Contacts>

Elements can also take an option, XSINIL, if you want to see all the elements, even those that have NULL values, then you want to add XSINIL, and you’ll get the elements, along with a XML tag defining the tag as null.  I’m not sure why you’d want this, since not there and NULL, are pretty much the same, but I’m sure someone has a parser that will blow up if you don’t include every element.

So Let’s see the query:

SELECT TOP 5
     ContactID, Title, FirstName, MiddleName, LastName, Suffix
FROM Person.Contact
FOR XML RAW('Contact'), ROOT('Contacts'), ELEMENTS XSINIL
--one last time clicking the link
<Contacts xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
 <Contact>
  <ContactID>1</ContactID>
  <Title>Mr.</Title>
  <FirstName>Gustavo</FirstName>
  <MiddleName xsi:nil="true" />
  <LastName>Achong</LastName>
  <Suffix xsi:nil="true" />
 </Contact>
 <Contact>
 <ContactID>2</ContactID>
  <Title>Ms.</Title>
  <FirstName>Catherine</FirstName>
  <MiddleName>R.</MiddleName>
  <LastName>Abel</LastName>
  <Suffix xsi:nil="true" />
 </Contact>
 <Contact>
  <ContactID>3</ContactID>
  <Title>Ms.</Title>
  <FirstName>Kim</FirstName>
  <MiddleName xsi:nil="true" />
  <LastName>Abercrombie</LastName>
  <Suffix xsi:nil="true" />
 </Contact>
 <Contact>
  <ContactID>4</ContactID>
  <Title>Sr.</Title>
  <FirstName>Humberto</FirstName>
  <MiddleName xsi:nil="true" />
  <LastName>Acevedo</LastName>
  <Suffix xsi:nil="true" />
 </Contact>
 <Contact>
  <ContactID>5</ContactID>
  <Title>Sra.</Title>
  <FirstName>Pilar</FirstName>
  <MiddleName xsi:nil="true" />
  <LastName>Ackerman</LastName>
  <Suffix xsi:nil="true" />
 </Contact>
</Contacts>

If you hit up the books online, You’ll find three more options.  I didn’t see anything about this in the Self-Paced book.  I don’t recall seeing any questions on it either but, for completeness:

If you wanted to include a schema in your results you have two options, XMLDATA or XMLSCHEMA.  If you want to use these, you cannot name your row elements (don’t pass a name to the RAW(”) option, and you cannot wrap your results in a ROW(‘name’).

If you’re going to return binary data (images anyone?), then you’ll have to use the BINARY BASE64 option.  That way it’ll encode the binary data so it can be included in your XML.

Now, that’s all the options I can find for the FOR XML RAW statement.

  • RAW accepts a name parameter, RAW(‘name’), that name will be the element name for each row.
  • You can add ROOT(‘name’), this will wrap the entire results set in a root named whatever you pass.
  • If you want to change the columns from attributes of each row element, to children elements, add the ELEMENTS keyword.
  • If you want to show every element, even those storing NULL values, add the XSINIL keyword.
  • You have two options for including schema data, XMLDATA and XMLSCHEMA, but be warned you can’t use a root node, or name your row elements.
  • If you’re going to include binary data in your results, be sure to add the BINARY BASE64 option.

That’s it!  Next time, we’re going to cover FOR XML AUTO.  Fewer options, but it handles hierarchical data (at least simple hierarchical data) right out of the box.

Play around with these options.  Learn when you want to include each of the options, and you’ll be better prepared for the 70-433!

Do you have any questions?  If so, send them in.  I’m here to help in any way that I can!

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recent Posts

  • A New File Interrogator
  • Using Generative AI in Data Engineering
  • Getting started with Microsoft Fabric
  • Docker-based Spark
  • Network Infrastructure Updates

Recent Comments

  1. slowder on Data Engineering for Databricks
  2. Alex Ott on Data Engineering for Databricks

Archives

  • July 2023
  • June 2023
  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • October 2018
  • August 2018
  • May 2018
  • February 2018
  • January 2018
  • November 2017
  • October 2017
  • September 2017
  • August 2017
  • June 2017
  • March 2017
  • February 2014
  • January 2014
  • December 2013
  • November 2013
  • October 2013
  • August 2013
  • July 2013
  • June 2013
  • February 2013
  • January 2013
  • August 2012
  • June 2012
  • May 2012
  • April 2012
  • March 2012
  • February 2012
  • January 2012
  • December 2011
  • November 2011
  • October 2011
  • September 2011
  • August 2011
  • July 2011
  • June 2011
  • May 2011
  • April 2011
  • March 2011
  • February 2011
  • January 2011
  • December 2010
  • November 2010
  • October 2010
  • September 2010
  • August 2010
  • July 2010
  • June 2010
  • May 2010
  • April 2010
  • March 2010
  • January 2010
  • December 2009
  • November 2009
  • October 2009
  • September 2009
  • August 2009
  • July 2009
  • June 2009
  • May 2009
  • April 2009
  • March 2009
  • February 2009
  • January 2009
  • December 2008
  • November 2008
  • October 2008
  • September 2008
  • August 2008
  • July 2008
  • June 2008
  • May 2008
  • April 2008
  • March 2008
  • February 2008
  • January 2008
  • November 2007
  • October 2007
  • September 2007
  • August 2007
  • July 2007
  • June 2007
  • May 2007
  • April 2007
  • March 2007
  • February 2007
  • January 2007
  • December 2006
  • November 2006
  • October 2006
  • September 2006
  • August 2006
  • July 2006
  • June 2006
  • May 2006
  • April 2006
  • March 2006
  • February 2006
  • January 2006
  • December 2005
  • November 2005
  • October 2005
  • September 2005
  • August 2005
  • July 2005
  • June 2005
  • May 2005
  • April 2005
  • March 2005
  • February 2005
  • January 2005
  • November 2004
  • September 2004
  • August 2004
  • July 2004
  • April 2004
  • March 2004
  • June 2002

Categories

  • Career Development
  • Data Engineering
  • Data Science
  • Infrastructure
  • Microsoft SQL
  • Modern Data Estate
  • Personal
  • Random Technology
  • uncategorized
© 2025 shannonlowder.com | Powered by Minimalist Blog WordPress Theme