The RAW mode takes the query result and transforms each row
in the result set into an XML element with a generic
identifier row as the element tag and the columns in the
SELECT as attributes.
The AUTO mode returns query results in a simple, nested XML
tree. Each table in the FROM clause for which at least one
column is listed in the SELECT clause is represented as an
XML element. The columns listed in the SELECT clause are
mapped to the appropriate element’s attributes. The columns
can be optionally mapped to subelements. The nesting of the
elements or hierarchy in the result set is based on the
order of tables identified by the columns specified in the
SELECT clause. The leftmost table will be the top element.
The second leftmost table (identified by columns in the
SELECT statement) will be nested within the top element, and
In EXPLICIT mode, you can explicitly define the shape of the
resulting XML tree. Using this mode requires that the
queries be written in a specific way, so that additional
play with given query.
IF OBJECT_ID('tempdb..#XmlTestTable') IS NOT NULL DROP TABLE #XmlTestTable CREATE TABLE #XmlTestTable ( ID INT PRIMARY KEY IDENTITY(1,1), FirstName VARCHAR(20), LastName VARCHAR(20) ) INSERT INTO #XmlTestTable (FirstName,LastName) VALUES ('John','Doe'), ('Jane','Doe'), ('Brian','Smith'), ('Your','Mom') --YOUR TESTS SELECT * FROM #XmlTestTable FOR XML AUTO SELECT * FROM #XmlTestTable FOR XML RAW SELECT * FROM #XmlTestTable FOR XML RAW, ELEMENTS SELECT * FROM #XmlTestTable FOR XML PATH('Customers') DROP TABLE #XmlTestTable
information about the desired nesting is specified
explicitly as part of the query.