© Peter A. Carter  2018
Peter A. CarterSQL Server Advanced Data Typeshttps://doi.org/10.1007/978-1-4842-3901-8_3

3. Constructing XML with T-SQL

Peter A. Carter1 
London, UK

T-SQL allows you to convert relational results sets into XML, by using the FOR XML clause in your SELECT statement. There are four modes that can be used with the FOR XML clause; FOR XML RAW, FOR XML AUTO, FOR XML PATH, and FOR XML EXPLICIT. This chapter will demonstrate how the FOR XML clause works in RAW mode, AUTO mode, PATH mode, and EXPLICIT mode. As the chapter progresses, we will move from simple to more complex examples.


The simplest and easiest to understand of the FOR XML modes is FOR XML RAW. This mode will transform each row in a relational result ...

Get SQL Server Advanced Data Types: JSON, XML, and Beyond now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.