How to Manipulate XML data in Database Table, SQL

With a growth in clients accessing data through heterogeneous hardware and software platforms, a need across for a language that could be interpreted by any environment. This resulted in the evolution of a language called XML. SML is a mark-up language that is used to describe the structure of data in a standard hierarchical manner.

The structure of the documents containing the data is described with the help of tags contained in the document. Therefore, various business applications store their data in XML documents.

SQL Server allows you to save or retrieve data in the XML format. This enables the SQL Server to provide database support to various kinds of applications. As a database developer, it is important for you to learn to manipulate the XML data by using SQL Server.
The structure of the XML data can be defined in the form of a supporting Document Type Definition (DTD) or schema. You can read more about XML in the appendix.

Staring XML Data in a Table

The XML data is available in the form of XML fragments or complete XML documents. An XML fragment contains XML data without a top-level element that contains the complete data.

SQL Server 2005 uses XML as a data type to save the XML data in its original state. You can create tables or variables by using this data type to store the XML data. However, you can also shred the XML data and store the values in different columns in a rowset. This process of transforming the XML data into a rowset is called as shredding.

In SQL Server, you can store the XML data in the following ways:

  • A rowset
  • An XML column

Staring the XML Data in a Rowset

Consider an example. You have received the order details from a vendor. The order details are generated by the application used by the vendor in an XML document. You need to store this data in a database table. For this, you need to shred the XML data. The SQL Server allows you to shred the XML data by using the OPENXML function and its related stored procedures.

Shredding an XML document involves the following tasks:

Rhett Butler

Some say he’s half man half fish, others say he’s more of a seventy/thirty split. Either way he’s a fishy bastard. Google