Send XML to Procedure and Insert into DATABASE

SomeTimes we have Requirements to insert xml to Database.for this we send the whole xml file as parameter to store procedure.
Lets Start insert Xml Data to Database Step By Step.
for Example we have a sample XML which we want to insert into table.

<Table>
   <Rows fileno="1" filetype="txt" pagecount="10">
    <filename>txtfilename</filename>    
    <source>txtfilesource</source>
  </Rows>
  <Rows fileno="2" filetype="pfd" pagecount="15">
    <filename>pdffilename</filename>
    <source>pdffilesource</source>
  </Rows>
</Table>

Step 1: Create the Table

Create Table tblDemoTable
(
sno   int  identity(1,1),
fileno int,
filetype varchar(10),
pagecount int,
filename varchar(50),
source varchar(50)
)

Step 2: Create Store Procedure

Create PROCEDURE usp_insertXmlToDb
@Xmldata xml
AS
BEGIN
DECLARE @idoc int
EXEC sp_xml_preparedocument @idoc OUTPUT, @xmldata 


 INSERT INTO tblDemoTable                                                                                               
      SELECT                                       
      fileno                                                              
     ,filetype                                                    
     ,pagecount                                                                        
     ,filename                                                                        
     ,source                                                                                                                                             
FROM   OPENXML (@idoc, '/Table/Rows',2)                                                      
     WITH ( fileno int '@fileno',                                                        
     filetype       varchar(10) '@filetype',                             
     pagecount  int '@pagecount',                                                        
     dt  varchar(150) '@dt',                                                                                                              
     filename varchar(50) './filename/text()',                                      
     source varchar(max) './source/text()'                                                                                                            
    )                                            
EXEC sp_xml_removedocument @idoc 
END

Step 3: Execure the Store Procedure

usp_insertXmlToDb'<Table>
   <Rows fileno="1" filetype="txt" pagecount="10">
    <filename>txtfilename</filename>    
    <source>txtfilesource</source>
  </Rows>
  <Rows fileno="2" filetype="pfd" pagecount="15">
    <filename>pdffilename</filename>
    <source>pdffilesource</source>
  </Rows>
</Table>'

Step 4: check the Table Data

select * from tblDemoTable
Advertisements
This entry was posted in Asp.Net, Sql and tagged , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s