SIGN UP MEMBER LOGIN:    
ARTICLE

Bulk DML With SQL Server 2005 - XML Data Type

Posted by Sridhar Subramanian Articles | SQL Server 2012 October 15, 2008
This article explains about doing bulk DML on SQL Server 2005 using XML datatype
Reader Level:

I would like to show how we can use SQL Server 2005's XML datatype for inserting / updating / deleting multiple records. When we have 100's of records which has to be processed in SQL server 2005 best way of doing it is passing the records as a XML instead of looping it from the front end and calling the Stored procedure N number of times. Below are the steps to use XML effectively in SQL server programming..

Step 1:

Create a stored procedure with XML Data type as a input paramter

Step 2:


Count the number of rows in passed XML parameter ( Specify the XML Schema inside )

@TotalNoOfRecords = @insertxml.query('<count>{count(/Info/employee)}</count>').value('count[1]','int')


Step 3:


Write your DML statement inside the "WHILE" loop ,

INSERT INTO tblXMLTest(name,city) Sample Insertion

SELECT InsertXML.value('@name[1]','varchar(40)'), insertxml.value('@city[1]','varchar(40)') FROM @InsertXML.nodes('/Info/employee[position()=sql:variable("@i")]') e(InsertXML)     

Complete example of using XML Datatype for Bulk XML Insert / Update / Delete

Sample SQL Code

DECLARE @InsertXML XML -- Which will be supplied from stored procedure

SET @InsertXML = '<Info>
  <employee name="Sridhar" city="Boston"></employee>
  <employee name="Subramanian" city="Dallas"></employee>
  <employee name="JoeBiden" city="New York"></employee>
</Info>'

DECLARE @TotalNoOfRecords INT, @i INT
SELECT @TotalNoOfRecords = @insertxml.query('<count>{count(/Info/employee)}</count>').value('count[1]','int')
SET @i=1
WHILE @i <=@TotalNoOfRecords

BEGIN
    INSERT
INTO tblXMLTest(name,city) Sample Insertion
    SELECT InsertXML.value('@name[1]','varchar(40)'), insertxml.value('@city[1]','varchar(40)')
   
FROM @InsertXML.nodes('/Info/employee[position()=sql:variable("@i")]') e(InsertXML)
-- Increment Loop
    SET
@i = @i + 1
END
 

Login to add your contents and source code to this article
share this article :
post comment
 
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.
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
Team Foundation Server Hosting
Become a Sponsor