There are times when using the XML data type in Microsoft SQL Server can be of great help. For example the XML data type is very useful when you want to return data in a hierarchical structure rather than a simple tabular structure.
There are several options for going about and returning XML from your queries rather than tabular data, but I will take a short look into the pros and cons of the two most popular options. These are: FOR XML AUTO and FOR XML PATH.
A point to note is that the XML returned in any of these queries will not output nodes if NULL database data is involved. That can potentially cause problems if you are expecting a specific XML structure at your front-end (ie: in your XSLT file). So if you want to return blank nodes as part of your XML you need to use the command: ELEMENTS XSINIL. Note that this forces your XML to return as nodes rather than the default syntax which returns the tables you are selecting from as nodes, and the data returned as attributes of your node.
(more…)