SQL 301-FOR XML EXPLICIT
OK, so far I’ve covered FOR XML RAW. It’s a pretty simple way to crank out some XML when you need to. FOR XML AUTO has better support for hierarchies, but it too has it’s limits. The end all be all for extensibility is FOR XML EXPLICIT. It actually supports some options that you can’t even do with FOR XML PATH.
The real difficulty with working with FOR XML EXPLICIT comes from the fact you have to build your result set in a specific way; you have to name the columns in a specific way; finally, you must also order your results in a specific way to get the results you’re looking for. Let’s dig into this a bit more.
First off, your results must always contain columns called tag and parent. The tag column holds an integer for each xml element you want to return. Parent refers to the tag you want the current tag to belong to… as in a parent – child relationship. When you want the element to be the top most result (it doesn’t have a parent) you use NULL as the parent value.
Let’s build an XML showing off Customers and their orders in the Adventureworks database.
Let’s go through this query taking it a piece at a time. The first sub query in my whole query sets up the basic structure. For the first element ( or root element) we want Customers, to do that, we set up 1 as the tag (that’s like 2), NULL as the parent (on line 3), then we name it Customers, and mark it to be an element (on line 4). Customers has no attributes, so the “element” tells the interpreter just make the element, there aren’t going to be any attributes.
Now, for the second element, Customer. We want to set it up to have two attributes, ID, and AccountNumber. So we build line 5 to create The ID attribute for Customer, and line 6 builds the AccountNumber attribute for Customer.
Finally we build the 3rd element, Order. It has two attributes, ID and OrderDate. So we build those on lines 7 and 8.
You should get the idea of XML construction using FOR XML EXPLICIT. You build the Elements and Attributes with “ElementName!Tag!AttributeName (if applicable, otherwise just element).
Let’s move on to the second sub query. This one builds the data for the Customer elements. Since Customer is the second element, we choose 2 as Tag ( in line 11). Customer is the child element of Customers (tag 1), so we enter 1 as the parent (line 12). Since we’re populating data for element 2, we aren’t going to enter data for the Customers element, so we want to skip that for this query…enter NULL for the customer’s element’s data (line 13).
Now we want to populate the data for Customer!2!ID and Customer!2!AccountNumber. You can see in lines 18 and 19, we’re querying the Sales.customer Data (and limiting it to the first two customers), and we fill in the data on lines 14 and 15. We put CustomerID from the Sales.Customer table into the attribute Customer!2!ID, and we put AccountNumber from Sales.Customer into the Customer!2!AccountNumber Attribute.
Again, since this sub query only deals with Customer Information, we pass NULL for the two Order Attibutes.
Now, for the final query. We’re going to fill in Order attributes in this query, so we mark these results as Tag 3, 2 is the parent this time (the customer element), The Customer’s element is still null here, but this time we are going to fill in the CustomerID.
The reason we do that is so the ordering is going to work later. If we don’t put in a value for CustomerID in this last query, the results won’t come out right. Remember I said your results had to be ordered in a specific way? This is what I was referring to. Try this change line 25 to NULL as “Customer!2!ID”. What do you get?
This error is basically telling you it can’t order the results in such a way that it can construct your XML. When I’ve seen it, it’s usually because I’ve failed to relate one sub query to the rest. Look for this when you get that error, and you’ll be fine.
Now, since we’re in the third sub query, we’ll be populating Order!3!ID and Order!3!OrderDate, so we fill those in on lines 27 and 28, using Sales.SalesOrderHeader.
Finally, on line 31, we mark the order we want to build this XML, we want to order by Customer!2!CustomerID (even though in the third sub query we get the customer ID from SalesOrder Header). After sorting by CustomerID, we sort by Tag, so the hierarchy comes out the way we want.
After that, all you have to do is tell the interpreter you want this rendered in XML Explicit mode, and viola, you’re done!
You’ll get a response that looks something like this.
This is the mode I use most often when I have to build the results in a VERY specific way. Otherwise I usually will fall back to raw just to see the output in XML.
It may take a little bit to get this under your belt, I will admit, I don’t use XML output every day, when I do, I usually have to hit up a reference to make sure I get it just right. But that won’t help you in the 70-433. Practice with this, try to break it. Really dig into it…you’ll get this in no time.
Next time, we’ll cover the FOR XML PATH option. It’s supposed to make building XML result sets easier. I’ll leave that to you to judge… As usual if you have any questions, please send them in. I’m here to help.