Multitable SELECT with FOR XML

Multitable SELECT with FOR XML

FOR XML SELECT statements are not limited to single-table SELECTs—they can be applied to joins as well. For example, we can use the following SQL join query to produce a list of VRG customers and the artists they are interested in:

/* *** SQL-Query-CH12-03 *** */ SELECT

CUSTOMER.LastName AS CustomerLastName, CUSTOMER.FirstName AS CustomerFirstName, ARTIST.LastName AS ArtistName

FROM

CUSTOMER, CUSTOMER_ARTIST_INT, ARTIST

WHERE

CUSTOMER.CustomerID = CUSTOMER_ARTIST_INT.CustomerID

AND

CUSTOMER_ARTIST_INT.ArtistID = ARTIST.ArtistID

ORDER BY

CUSTOMER.LastName, ARTIST.LastName

FOR

XML AUTO, ELEMENTS;

Figure 12-12(a) shows the query in the Microsoft SQL Server Management Studio, and Figure 12-12(b) shows the full results in a tabbed window. Figure 12-12(c) shows results in an XML document.

SQL Server uses the order of the tables in the FROM clause to determine the hierarchical placement of the elements in the generated XML document. Here, the top-level element is CUSTOMER, and the next element is ARTIST. The CUSTOMER_ARTIST_INT table does not appear in the generated document because no column from that table appeared in the SQL SELECT statement.

You can write the expression FOR XML AUTO, XMLDATA to cause SQL Server to produce an XML Schema statement in front of the XML document that it writes. The schema produced, however, involves topics that we will not cover in this chapter, so we will not do that.

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-12

FOR XML AUTO ELEMENTS Example Displaying Customer and Artist Interests

(a) FOR XML AUTO, ELEMENTS Query

Chapter 12 Database Processing with XML

The SQL FOR XML AUTO, ELEMENTS query results

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

Figure 12-12

Continued (continued)

Part 5 Database Access Standards

Figure 12-12

Continued

Chapter 12 Database Processing with XML

Figure 12-12

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

An XML Schema for the XML document in Figure 12-12(c) is shown in Figure 12-13(a), and Figure 12-13(b) shows the graphical display of this same schema. Observe that the MyData element can have an unbounded number of CUSTOMER elements, and each CUSTOMER can have an unbounded number of ARTIST elements, one for each artist interest. In this figure, the notation 1..α means that at least one CUSTOMER is required and an unlimited number will be allowed.

Part 5 Database Access Standards

(a) XML Schema

(b) Graphical Representation of the XML Schema

Figure 12-13

Customer and Artist

An XML Schema for All CUSTOMER Purchases

Interests

Suppose now that we want to produce a document that has all of the customer purchase data. To do that, we need to join CUSTOMER to TRANS to WORK to ARTIST and select the appro- priate data. The following SQL statement produces the required data:

/* *** SQL-Query-CH12-04 *** */ SELECT

CUSTOMER.LastName AS CustomerLastName, CUSTOMER.FirstName AS CustomerFirstName, TRANS.TransactionID, SalesPrice, WORK.WorkID, Title, Copy, ARTIST.LastName AS ArtistName

Chapter 12 Database Processing with XML

FROM

CUSTOMER, TRANS, [WORK], ARTIST

WHERE

CUSTOMER.CustomerID = TRANS.CustomerID

AND

TRANS.WorkID = WORK.WorkID

AND

WORK.ArtistID = ARTIST.ArtistID

ORDER BY

CUSTOMER.LastName, ARTIST.LastName

FOR

XML AUTO, ELEMENTS;

Figure 12-14

Figure 12-14(a) shows the query in the Microsoft SQL Server Management Studio, and Figure 12-14(b) shows the full results in a tabbed window. Figure 12-14(c) shows partial results

FOR XML AUTO ELEMENTS

in an XML document (this is a very long XML document).

Displaying Customer Purchases

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

(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

Part 5 Database Access Standards

Figure 12-14

Continued

Chapter 12 Database Processing with XML

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

Figure 12-14

Continued

Figure 12-15(a) shows an XML Schema document for this SQL statement, and a graphical view of it is shown in Figure 12-15(b). According to the XML Schema in Figure 12-15(b), a CUSTOMER has from zero to unlimited TRANS elements, a TRANS element has exactly one WORK element, and a WORK element has exactly one ARTIST element.