17.07.2015 Views

SQL Server Execution Plans - Grant Fritchey - SQLServerCentral.com

SQL Server Execution Plans - Grant Fritchey - SQLServerCentral.com

SQL Server Execution Plans - Grant Fritchey - SQLServerCentral.com

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 7: Special Datatypes and <strong>Execution</strong> <strong>Plans</strong>FOR XMLIf you want to output the result of a query in XML format, you use the FOR XML clause.You can use the FOR XML clause in any of the following four modes:• AUTO – Returns results as nested XML elements in a simple hierarchy (think: table =XML element).• RAW – Transforms each row in the results into an XML element, with a generic identifier as the element tag.• EXPLICIT – Allows you to define explicitly, in the query itself, the shape of theresulting XML tree.• PATH – A simpler alternative to EXPLICIT for controlling elements, attributes, and theoverall shape of the XML tree.Each of these methods requires different T-<strong>SQL</strong> in order to arrive at the same output, andeach is associated with different performance and maintenance issues. We will explore allfour options and I'll point out where each has strengths and weaknesses.Just to introduce the capabilities that are possible with FOR XML, you can see that thefirst example, in Listing 7.1, produces a list of employees and their addresses. There isno requirement for any type of direct manipulation of the XML output, and the query issimple and straightforward, so we'll use XML AUTO mode.SELECT p.FirstName ,p.LastName ,e.Gender ,a.AddressLine1 ,a.AddressLine2 ,a.City ,a.StateProvinceID ,a.PostalCodeFROM Person.Person pINNER JOIN HumanResources.Employee e257

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!