Welcome to weblogs.com.pk
Sign in
|
Join
|
Help
Search
Nasir Ali Khan
Believing is easier than thinking. Hence so many more believers than thinkers.
Home
Email
About
RSS 2.0
Atom 1.0
Recent Posts
First look at ASP.NET 4.0
Windows Live Communications Platform has stopped working
Google Music Search Feature, Rocks!!!
2012 the beginning of end?
Microsoft offer 15-25% discount on various exams
Tags
.NET framework
ADO.NET
ASP.NET 2.0
C#
Cosmology
General Thoughts
Google
Microsoft Technologies
Science & Technology
Sociology
Sports
SQL Server
Web Applications
Web Technologies
Windoz Applications
Navigation
Home
Blogs
Forums
Photos
Downloads
My Reader
Control Panel
Archives
November 2009 (2)
October 2009 (4)
September 2009 (2)
May 2009 (1)
January 2009 (2)
November 2008 (1)
October 2008 (7)
September 2008 (4)
July 2008 (4)
June 2008 (2)
May 2008 (8)
April 2008 (2)
March 2008 (8)
February 2008 (16)
January 2008 (6)
December 2007 (5)
November 2007 (3)
October 2007 (2)
September 2007 (2)
August 2007 (7)
July 2007 (3)
June 2007 (8)
February 2007 (3)
November 2006 (1)
September 2006 (1)
July 2006 (2)
June 2006 (1)
May 2006 (3)
April 2006 (3)
March 2006 (4)
February 2006 (5)
December 2005 (2)
November 2005 (2)
October 2005 (2)
September 2005 (6)
August 2005 (5)
July 2005 (3)
June 2005 (6)
May 2005 (8)
April 2005 (4)
March 2005 (2)
February 2005 (5)
January 2005 (4)
An introduction to SQL Server’s OPENXML
Intended Readers:
Persons having expertise in SQL Server, T-SQL, stored procedures and XML
Level:
Expert
Introduction
OpenXML is basically a function of SQL Server which allows XML document to be viewed as rowset without any involvement of high level programming code. This rowset can be used in SQL statements such as Insert, Delete … etc.
Declaration of OPENXML is
OPENXML (idoc, int [in], rowPattern nvarchar [in], [flags byte [in]])
[WITH (SchemaDeclration | TableName)]
idoc:
a document handle to be parsed as XML. This handle is created by calling sp_xml_preparedocument stored procedure.
rowPattern:
as XPath pattern specifying the node of XML document to be processed as a rowset. i.e. N ’/Top/Region’ referes to Region node to process.
Flag:
indicates how XML node is to be interpreted. 1 indicates that attributes in the document becomes columns etc.
Examples
Following T-SQL code inserts two records NorthWind.Region table using OPENXML function
Declare @xmlDoc nvarchar(4000)
Declare @docIndex int
-- Text to be parsed as XML
set @xmlDoc = N'
<Top>
<Region RegionID="11" RegionDescription="Uptown" />
<Region RegionID="22" RegionDescription="Downtown" />
</Top>'
-- Prepare XML document in memory
execute sp_xml_preparedocument @docIndex OUTPUT, @xmlDoc
Insert Region
Select RegionID, RegionDescription
FROM OPENXML(@docIndex, N'/Top/Region', 1) With Region
-- Release XML document from memory
execute sp_xml_removedocument @docIndex
Let’s look at another example but this time for deleting the record from NorthWind.Region Table
Declare @xmlDoc nvarchar(4000)
Declare @docIndex int
set @xmlDoc = N'
<Top>
<Region RegionID="11" RegionDescription="Uptown" />
</Top>'
execute sp_xml_preparedocument @docIndex OUTPUT, @xmlDoc
Delete Region
FROM OPENXML(@docIndex, N'/Top/Region', 1) With Region as XMLRegion
Where Region.RegionID = XMLRegion.RegionID
execute sp_xml_removedocument @docIndex
Following T-SQL code is for updating the record with regionID=22 with the new Description value
Declare @xmlDoc nvarchar(4000)
Declare @docIndex int
set @xmlDoc = N'
<Top>
<Region RegionID="22" RegionDescription="NewRegionValue" />
</Top>'
execute sp_xml_preparedocument @docIndex OUTPUT, @xmlDoc
Update Region
Set Region.RegionDescription = XMLRegion.RegionDescription
FROM OPENXML(@docIndex, N'/Top/Region', 1) With Region as XMLRegion
Where Region.RegionID = XMLRegion.RegionID
execute sp_xml_removedocument @docIndex
Benefits of OPENXML
Rich support for XML processing.
No need for object to database mapping, since objects can be serialized in XML and can directly manipulate by the Database.
Runs in database process, results improved performance
Use of XML removes the language dependency
Reference
: Professional ADO.NET, Wrox Press, ISBN (1-861007-33-7)
Your comments and feedbacks are always welcome
Posted:
Wednesday, May 25, 2005 1:04 AM by
nasir
Comments
khurram
said:
Nice stuff...
Thanks for sharing it.
#
May 28, 2005 8:33 PM
Anonymous comments are disabled