C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Code Snippets » SQL »

Bulk Insert using XML Data - SQL Server


Posted Date: 13 Aug 2009    Resource Type: Code Snippets    Category: SQL
Author: srinathMember Level: Silver    
Rating: 1 out of 5Points: 7



We can do bulk insert in sql server using xml data. Please go through the sample given below, which self explantory.

Sample:
CREATE PROCEDURE InsertEmployees
/* ----------------------------------------------------------
Parameters
In :
@strXML = contains employee list as XML
Example: '


'
---------------------------------------------------------- */
@strXML varchar(8000)
AS

Declare @intPointer int

exec sp_xml_preparedocument @intPointer output, @strXML

INSERT into employee

SELECT EmpName, EmpSalary

FROM OpenXml(@intPointer,'/root/emp',2)

WITH EmpName varchar(20) '@EmpName' , EmpSalary varchar(20) '@EmpSalary')

exec sp_xml_removedocument @intPointer


-----------------------


EXEC InsertEmployees ''

sp_xml_preparedocument

Reads the XML text provided as input, parses the text by using the MSXML parser (Msxmlsql.dll), and provides the parsed document in a state ready for consumption. This parsed document is a tree representation of the various nodes in the XML document: elements, attributes, text, comments, and so on.

sp_xml_preparedocument returns a handle that can be used to access the newly created internal representation of the XML document. This handle is valid for the duration of the session or until the handle is invalidated by executing sp_xml_removedocument.


OPENXML

OPENXML provides a rowset view over an XML document. Because OPENXML is a rowset provider, OPENXML can be used in Transact-SQL statements in which rowset providers such as a table, view, or the OPENROWSET function can appear.

Sample:

OpenXml(@intPointer,'/root/emp',2)

WITH EmpName varchar(20) '@EmpName' , EmpSalary varchar(20) '@EmpSalary')



sp_xml_removedocument

Removes the internal representation of the XML document specified bythe document handle and invalidates the document handle. To avoidrunning out of memory, will run sp_xml_removedocument to free up thememory.


For more details, visit http://blogger.code4asp.net/2009/08/12/bulk-insert--sql-server.aspx



Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
XML Data  .  SQL Server  .  Sp_xml_removedocument  .  Sp_xml_preparedocument  .  OpenXml  .  Bulk Insert  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Copy a table and that content into another table
Previous Resource: Tables' name with record count - SQL Server
Return to Discussion Resource Index
Post New Resource
Category: SQL


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use