SIGN UP MEMBER LOGIN:    
ARTICLE

Openxml - XML Integration with SQL Server

Posted by Rekha Articles | SQL Server 2012 July 20, 2009
In this article we will know about Openxml and how to perform operation using it.
Reader Level:




What is OpenXML

  • SQL Server 2000 provides a system-defined function, OpenXML, that creates Rowsets from XML documents.
  • OPENXML allows the data in XML document to be treated just like the columns and rows of your database table
  • The OPENXML function allows developers to parse XML data so that it can be stored as relational data in tabular form. This function supports the XML data type and the sp_xml_preparedocument system stored procedure accepts this new data type. This procedure is used by OPENXML function. Whereas SQL Server 2000 allowed the use of varchar, nvarchar, text or ntext variables to generate a document handle using the abovementioned stored procedure, SQL Server 2005 allows developer to use the xml variable additionally.

Advantages of Using OPENXML
  • Inserting from XML is faster when using openxml
  • OPENXML provides an easy way to use an XML document as a data-source for your procedures.

OPENXML limitations

OPENXML is very memory intensive. The pointer returned by the system stored procedure sp_xml_preparedocument is a memory pointer to a COM XML document object model. So, you must be careful not to load very large XML documents into memory with OPENXML because it may overload your server's memory.

Example 1- Inserting records from XMLDoc to sql table

DECLARE @h int

DECLARE @xmldoc VARCHAR(1000)
--xmldoc is set with the xml elements which are to be inserted into the table students with FirstName,ID,Technology as table columns

SET @xmldoc =
'<root>
<student FirstName="Ravi" ID="1" Technology="DotNet"></student>

<student FirstName="Avdesh" ID="2" Technology="DotNet"></student> </root>'

EXEC sp_xml_preparedocument @h OUTPUT, @xmldoc

--This sp_xml_preparedocument is internal server SP (pseudo SP). which takes the xmldoc as input and gives an output in @h which contains the data which is to be manipulated further

INSERT INTO student
SELECT * FROM OpenXML(@h,'/root/student')
WITH student
EXEC sp_xml_removedocument @h
--sp_xml_removedocument free's up the memory.

Output:

outputOpenXml.bmp
sp_xml_preparedocument can only process text or untyped XML. If an instance value to be used as input is already typed XML, first cast it to a new untyped XML instance or as a string and then pass that value as input.

A parsed document is stored in the internal cache of SQL Server. To avoid running out of memory, run sp_xml_removedocument to free up the memory.

Example 2 - Updating records from XMLDoc to sql table

DECLARE @h int

DECLARE @xmldoc VARCHAR(1000)
--xmldoc is set with the xml elements which are to be inserted into the table students with FirstName,ID,Technology as table columns

SET @xmldoc =
'<root>
<student FirstName="Ravi Sharma" ID="1" Technology="DotNet"></student>
<student FirstName="Avdesh" ID="2" Technology="DotNet"></student> </root>'

EXEC sp_xml_preparedocument @h OUTPUT, @xmldoc
--This sp_xml_preparedocument is internal server SP (pseudo SP). which takes the xmldoc as input and gives an output in @h which containd the data which is to be manipulated further

UPDATE student
SET
FirstName = x.FirstName
,ID = x.ID
,Technology = x.Technology
FROM OpenXML(@h,'/root/student')
WITH (FirstName nvarchar(20),ID nvarchar(20),Technology nvarchar(20)) x where student.ID='1'

EXEC sp_xml_removedocument @h
--sp_xml_removedocument free's up the memory.

select * from student

Output:

outputOPenXmlUpd.bmp
 

Login to add your contents and source code to this article
share this article :
post comment
 

Hi, This is a very simple and good example. Could you please quote another example which includes element centric mapping and also includes retrieving values from different XML path using OPENXML only once.

Posted by Ravinder Pal Singh Feb 01, 2011

Hi, This is a very simple and good example. Could you please quote another example which includes element centric mapping and also includes retrieving values from different XML path using OPENXML only once.

Posted by Ravinder Pal Singh Feb 01, 2011

guys, you can find out the exact solutions about openxml() function in sql server 2008 using following url,


Posted by spark mohideen Oct 09, 2009

Thank u dear

Posted by Rekha Jul 21, 2009

Good :) Its very clear and nice article 2 understand 

Posted by Dhananjay Kumar Jul 21, 2009
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Nevron Gauge for SharePoint
Become a Sponsor