New features about XML in SQL Server 2008

Source: Internet
Author: User
Tags microsoft sql server versions

1 Introduction

Microsoft has introduced XML-related features and Transact-SQL keyword FOR XML and OPENXML in Microsoft SQL Server 2000. This allows developers to write Transact-SQL code to obtain query results in the form of XML streams and to split an XML document into a rowset. SQL Server 2005 significantly expands these XML capabilities, and introduces a local XML data type that supports XSD schema validation, XQuery based operations, and XML indexing. Built on previous versions of XML functionality, SQL Server 2008 has been improved to meet the challenges that customers face when storing and manipulating XML data in a database.

2 Evolution of SQL Server XML functionality

SQL Server's XML capabilities evolve with each version since the SQL Server2000 version. Before we examine the improvements in SQL Server 2008, it may be useful to summarize the evolution of XML functionality through previous versions.

2.1 XML functionality in SQL Server 2000

In SQL Server 2000, Microsoft has launched the Transact-SQL keyword FOR XML and OPENXML. For XML is an extension of the SELECT statement that returns the query results of the XML stream as shown in the following example.

SELECT ProductID, ProductName
   FROM Products Product
   FOR XML AUTO

This query returns an XML fragment as shown in the following example:

<Product ProductID="1" ProductName="Widget"/>
<Product ProductID="2" ProductName="Sprocket"/>

The OPENXML feature performs the opposite of the FOR XML conditional clause, creating a rowset for an XML document, as shown in the following example:

DECLARE @doc nvarchar(1000)
SET @doc = '<Order OrderID = "1011">
 <Item ProductID="1" Quantity="2"/>
 <Item ProductID="2" Quantity="1"/>
 </Order>'
DECLARE @xmlDoc integer
EXEC sp_xml_preparedocument @xmlDoc OUTPUT, @doc
SELECT * FROM
OPENXML (@xmlDoc, 'Order/Item', 1)
WITH
(OrderID integer '../@OrderID',
ProductID integer,
Quantity integer)
EXEC sp_xml_removedocument @xmlDoc

Note the use of sp_xml_preparedocument and sp_xml_removedocument stored procedures to create a memory display of the node tree of the XML document. This Transact-SQL code returns to the rowset below.

OrderID ProductID Quantity
1011 1 2
1011 2 1

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.