From the course: Microsoft SQL Server 2016 Essential Training

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Write queries to return XML

Write queries to return XML - SQL Server Tutorial

From the course: Microsoft SQL Server 2016 Essential Training

Start my 1-month free trial

Write queries to return XML

- [Instructor] In Microsoft SQL Server, there's a number of ways we can work with XML. One of the easiest is to take data that's currently being stored in a table and display it as XML. So rather than display the data as columns and rows, we will instead return the data as XML. In order to do this, I'll start off with a general SQL select statement, in a new query window. We'll be working with the Application.People table and the columns I'm interested in are FullName, PreferredName, EmailAddress. So if we run this query as-is, the data will be returned to us in columns and rows. If we would prefer for that to be returned to us in XML, all we have to do is add a few keywords at the end of the statement. So I'm going to add FOR XML AUTO, A-U-T-O, and then run the query again. And now what's returned to me is XML. I'll click on it, and we'll see, we were returned one row of XML for each row in the database. And all of what had been in columns is now instead in attributes. So we have a…

Contents