Query SQL Server Database Table XML data using CROSS APPLY
SQL programmers can SELECT from XML data nodes stored in SQL Server table column with XML data type. Although it is very similar to query XML variable, querying XML data column requires the use of SQL CROSS APPLY join. Cross Apply enables to execute SQL XML query on all rows of the database table instead of running SQL Select query on single XML data.
In this SQL Server XML tutorial, I tried to explain and demonstrate to build SQL Select statement to list XML nodes with their text and attribute values stored in SQL Server sample database table column with the help of CROSS APPLY.
Insert Sample XML Data to SQL Server Table
Let's start. First create SQL Server table which has an XML data column to store XML data. My sample sql table name is Books and it has BooksXML data column with XML data type.
Now, SQL developers are ready to populate Books sql table with sample data. I used an XML data type variable @XMLSQL within INSERT commands to populate database table with sample data as follows.
Now in our sample database table Books, we have 3 rows. Each row stores related books data in XML format in table column BooksXML defined in XML data type.
Display XML Data to SQL Server Management Studio
If you query sample XML SQL table using a basic SQL SELECT statement, we won't be able to retrieve book details stored in XML data column. Here is a Select statement without using any SQL XML notation.
Convert XML to SQL Data Rows
If SQL programmer wants to read single row XML column, the SQL XML sample tutorial Query XML Data in SQL Server can be helpful. The XMl data column value can be read into XML variable, and this SQL Server XML variable can be queried for its nodes and XML attributes as follows.
SQL programmers can also check the SQL Server XML tutorial SQL Server XML query example for an alternative sample case.
Convert XML Table Column to SQL Data Rows using Cross Apply
Of course this SELECT is not the solution T-SQL developers are seeking for listing all book titles seperately included in XML data in SQL table XML data columns. To SELECT from a XML type SQL table column is more complex. But SQL programmers can query XML data column using SQL Server CROSS APPLY join.
Here is a sample Cross Apply SQL query listing each XML node seperately as a row in the return list of SELECT query.
Please note that the starting point of the SELECT query is the main table "Books" which contains the XML data column. Then using a CROSS APPLY join we can reach XML data column "BooksXML" and its XML nodes "book" and alias it.
In the SELECT list, programmers will see the repeating table columns from Books table and unique XML attribute and XML node text value named as Book Title.
What is interesting in this SELECT statement is that the CROSS APPLY enables T-SQL programmers to apply this technique to different XML hierarch levels formed of different XML nodes on the same SQL Server XML SELECT statement. I'll try to show this example on an other SQL XML tutorial.