Using the SQL SELECT . . . FOR XML Statement

Using the SQL SELECT . . . FOR XML Statement

SQL Server 2008 R2 uses the SQL SELECT . . . FOR XML statement to work with XML. Consider the following SQL statement:

/* *** SQL-Query-CH12-01 *** */ SELECT

XML RAW;

The expression FOR XML RAW tells SQL Server to place the values of the columns as attributes in the resulting XML document. Figure 12-10(a) shows an example of as FOR XML RAW query in the Microsoft SQL Server Management Studio. The results of the query are displayed in a sin- gle cell. Clicking this cell displays the results as shown in Figure 12-10(b). As expected, each column is placed as an attribute of the element named row. The complete output, edited as it would appear in an XML document (and with extra spaces in the attribute values removed) is shown in Figure 12-10(c).

The SQL FOR XML RAW query

The SQL FOR XML RAW query results— click this cell to display the results in a separate window

Figure 12-10

FOR XML RAW Examples

(a) FOR XML RAW Query

Part 5 Database Access Standards

Figure 12-10

Continued

The SQL FOR XML RAW query results

(b) FOR XML RAW Results in the Microsoft SQL Server Management Studio

(c) FOR XML RAW Results in XML Document

It is also possible to cause SQL Server to place the values of the columns into elements rather than attributes by using FOR XML AUTO, ELEMENTS. For example, we can display the data in the ARTIST table using the SQL query:

/* *** SQL-Query-CH12-02 *** */ SELECT

XML AUTO, ELEMENTS;

Figure 12-11(a) shows the query in the Microsoft SQL Server Management Studio, and Figure 12-11(b) shows the full results in a separate tabbed window. Here, each attribute value

Chapter 12 Database Processing with XML

appears as a separate element. The complete output, edited as it would appear in an XML document (and with MyData tags added to contain the entire data set) is shown in Figure 12-11(c). The XML Schema for this document is shown in Figure 12-11(d), and the graphical representa- tion of the XML Schema is shown in Figure 12-11(e).

Using another option, FOR XML EXPLICIT, you can cause SQL Server to place some columns into elements and others into attributes. For example, you might decide to place all column values except surrogate key values into elements and all surrogate key values into attributes. The justifi- cation for this design is that surrogate key values have no meaning to the users, so they are more like metadata than data. The means by which this is done is beyond the scope of this discussion. See FOR XML EXPLICIT in the SQL Server documentation for more information.

The SQL FOR XML AUTO, ELEMENTS query

The SQL FOR XML AUTO, ELEMENTS query results—click this cell to display the results in a separate window

Figure 12-11

FOR XML AUTO, ELEMENTS Examples

(a) FOR XML AUTO, ELEMENTS Query

The SQL FOR XML AUTO, ELEMENTS query results

(b) FOR XML AUTO, ELEMENTS Results in the Microsoft SQL Server Management Studio

Figure 12-11

Continued

(c) FOR XML AUTO, ELEMENTS Results in XML Document

(d) XML Schema

Figure 12-11

Continued (e) Graphical Representation of the XML Schema

Part 5 Database Access Standards