Published: 20 Nov 2006
By: Imran Nathani

In Part 1 of this article, we saw the basics of the xml datatype and retrieval of data from it. In this part we will see the use of the xml datatype in relational data tables and the retrieval of data from an xml datatype in relational form.

Introduction

The purpose of displaying data from the xml datatype in a relational form is to apply normal ADO.NET practices and other relational data management concepts easily.

  • Formation of Views from this formed relational data.
  • Retrieval of this data as dataset, recordset or simply raw XML again.
  • This data can easily be used with multiple data providers for further processing.

Displaying XML in Relational form

Let us begin with the same Invoices XML used in part 1. Suppose we would like to display that XML in relational form as displayed below:

 InvoiceId Date SalesPerson
1 10-Jan-2006 XYZ
2 11-Jan-2006 XYZ
3 11-Jan-2006 ABC

Then we simply execute the following code:

We can easily break the operations of the SELECT query above into two parts:

Part 1 retrieves the nodes from the Table (Column) form:

Here we retrieve all nodes from Invoices by using @xmldata .nodes('Invoices/Invoice'). We use the AS keyword to set an alias name to @xmldata as a table (Tabx), and a column(Colx).

Part 2 queries the data in the relational column.

Here we query Colx with the query method passing in an XQuery expression as the parameter. We also specify the alias name for the column with AS InvoiceId.

The CROSS APPLY clause

A CROSS APPLY is an INNER JOIN between a table and a table-valued function. This is a new feature of SQL Server 2005. The results are normally used to retrieve and display running totals. In previous version of SQL Server, the same was achieved by using a temp table.

Displaying xml datatype data in relational form

Let's assume we have a table in the database for the Purchases made by a Purchaser from many other different Sellers. Each Seller would issue a different electronic Invoice which would be stored in a raw XML form in the database and each Seller would assign different types of 'InvoiceId's, different format of dates etc. . Here, we have a table Purchases of that type with the following data.

CompanyId CompanyName Invoices
1 IMI International Inc. <Invoices>
<Invoice InvoiceId="1">
<Invoicedate>10-01-2006</Invoicedate>
<Salesperson id="1">XYZ</Salesperson>
</Invoice>
<Invoice InvoiceId="2">
<Invoicedate>11-01-2006</Invoicedate>
<Salesperson id="1">XYZ</Salesperson>
</Invoice>
<Invoice InvoiceId="3">
<Invoicedate>11-01-2006</Invoicedate>
<Salesperson id="2">ABC</Salesperson>
</Invoice>
</Invoices>
2 LADS International LLC. <Invoices>
<Invoice InvoiceId="55">
<Invoicedate>10-April-05</Invoicedate>
<Salesperson id="3">PQR</Salesperson>
</Invoice>
<Invoice InvoiceId="56">
<Invoicedate>11-June-05</Invoicedate>
<Salesperson id="2">ABC</Salesperson>
</Invoice>
<Invoice InvoiceId="57">
<Invoicedate>11-May-06</Invoicedate>
<Salesperson id="2">ABC</Salesperson>
</Invoice>
</Invoices>
3 Mail & Parcel <Invoices>
<Invoice InvoiceId="BY-105">
<Invoicedate>10-April-2005</Invoicedate>
<Salesperson id="3">PQR</Salesperson>
</Invoice>
<Invoice InvoiceId="AX-117">
<Invoicedate>11-June-2005</Invoicedate>
<Salesperson id="2">ABC</Salesperson>
</Invoice>
<Invoice InvoiceId="AB-232">
<Invoicedate>11-May-2006</Invoicedate>
<Salesperson id="2">ABC</Salesperson>
</Invoice>
</Invoices>

Hence our SELECT query for this table would be:

Our result for the above query would be:

CompanyName InvoiceId Date
IMI International Inc. 1 10-01-2006
IMI International Inc. 2 11-01-2006
IMI International Inc. 3 11-01-2006
LADS International LLC. 55 10-April-05
LADS International LLC. 56 11-June-05
LADS International LLC. 57 11-May-06
Mail & Parcel BY-105 10-April-2005
Mail & Parcel AX-117 11-June-2005
Mail & Parcel AB-232 11-May-2006

Summary

The display of XML data as relational tables becomes very important in cases where the front ends are classical in nature not supporting latest XML standards. This is also useful in the intermediate phases of an application upgrade for the easy compatibly of the phase-in components with phase-out components.

<<  Previous Article Continue reading and see our next or previous articles Next Article >>

About Imran Nathani

Sorry, no bio is available

This author has published 5 articles on DotNetSlackers. View other articles or the complete profile here.

Other articles in this category


Lucene.NET vs SQL Server Full-text – Generating a million records and a full-text index
In this article we will take a look at how SQL Server performs with one million records in a table. ...
Identifying currently running SQL queries
This article is taken from the book SQL Server DMVs in Action. The author discusses a routine that i...
What's blocking my running SQL?
This article is taken from the book SQL Server DMVs in Action. The author discusses a routine that i...
Easy Insert, Update and Retrieve Values for Microsoft SQL Database with C#, Visual Studio
This article will provide you with the small amount of code required to insert, update and retrieve ...
SQL Azure to Developers: Part 1
In this part we will focus on overview of SQL Azure along with a first look on SQL Azure Management ...

You might also be interested in the following related blog posts


An alternative to Crystal read more
Building a class browser with Microsoft Ajax 4.0 Preview 5 read more
Scenarios for WS-Passive and OpenID read more
Self-reference hierarchy with Telerik TreeView for Silverlight read more
Silverlight Twitter Client with authentication read more
How To: Display Hierarchical Data with Row Details (RadGridView for Silverlight) read more
Using Microsoft's Chart Controls In An ASP.NET Application: Plotting Chart Data read more
Business Apps Example for Silverlight 3 RTM and .NET RIA Services July Update: Part 7: ADO.NET Data Services Based Data Store read more
Dynamic Languages: A Separation of Concerns read more
A Bad Idea, EF Entities over WCF read more
Top
 
 
 

Discussion


Subject Author Date
placeholder Very good article Rajesh S 7/30/2008 6:06 AM

Please login to rate or to leave a comment.

Free Agile Project Management Tool from Telerik
TeamPulse Community Edition helps your team effectively capture requirements, manage project plans, assign and track work, and most importantly, be continually connected with each other.