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
Parts
» This page intentionally left blank
» Reporting and Data Mining Database Applications
» Database Applications and SQL
» Reading Specified Columns and Rows from a Single Table
» “Does Not Work with Microsoft Access ANSI-89 SQL”
» Processing SQL Statements in Microsoft Access 2010
» Using SQL in Microsoft SQL Server 2008 R2
» Using SQL in Oracle Database 11g
» Using SQL in Oracle MySQL 5.5
» Wildcards in SQL WHERE Clauses
» Using SQL Built-in Functions
» SQL Expressions in SQL SELECT Statements
» Querying Multiple Tables with Subqueries
» Querying Multiple Tables with Joins
» Comparing Subqueries and Joins
» Finding Functional Dependencies
» Eliminating Anomalies from Multivalued Dependencies
» The Multivalue, Multicolumn Problem
» The General-Purpose Remarks Column
» R Diagrams Using the IE Crow’s Foot Model
» The Multivalued Attribute Pattern
» The Archetype/Instance Pattern
» The Student Acceptance Letter
» X This is a warning, no further action is required.
» 1:1 Relationships Between Strong Entities
» M Relationships Between Strong Entities
» Relationships in Mixed Entity Designs
» Representing Ternary and Higher-Order Relationships
» Relational Representation of the Highline University Data Model
» Surrogate Key Database Design
» Column Properties for the View Ridge Database Design Tables
» Variations in SQL Data Types
» Implementing Data Constraints
» Populating the View Ridge Database Tables
» Using Triggers to Provide Default Values
» The WORK_AddWorkTransaction Stored Procedure
» • If a PROJECT row is deleted, then the project has been canceled, and it is unneces-
» Reducing Cardinalities (with Data Loss)
» Optimistic Versus Pessimistic Locking
» Declaring Lock Characteristics
» Processing Rights and Responsibilities
» Recovery via Rollback/Rollforward
» Maintaining the Data Repository
» Types of Distributed Databases
» • Express Edition. This free, feature-limited version is available for download. It
» SQL Server 2008 R2 SQL Statements and SQL Scripts
» Creating the View Ridge Database Table Structure
» Populating the VRG Tables with Data
» The Stored Procedure InsertCustomerAndInterests
» The Stored Procedure InsertCustomerWithTransaction
» A Trigger for Setting Default Values
» A Trigger for Enforcing a Data Constraint
» A Trigger for Enforcing a Required Child Constraint
» Creating an ODBC Data Source Name
» Materializing XML Documents with XSLT
» Using the SQL SELECT . . . FOR XML Statement
» Multitable SELECT with FOR XML
» A Schema with Two Multivalued Paths
» Problems with Operational Data
» Using SQL for Market Basket Analysis
Show more