Insert XML Data In Database Using Stored Procedure In SQL Server

Introduction
 
In this blog, I’ll explain how to insert data from XML Sheets using SQL server. A few days ago my client said they have data in XML format and want to insert all the records which are available in XML Format that should be inserted in the database. So, to achieve this kind of requirement for my client I wrote SQL queries and procedures which help me to insert all the records in bulk from XML Sheets. So, in this article, I am going to explain how to create stored procedure to insert all the records in the table from XML sheets. In this created stored procedure I simply was passing the complete XML data as a parameter. So let's start and take the example for demonstration.

CREATE TABLE
 
Let's create a Temp Table with the name #tblXMLData to store XML Data. In this Table, I will take 4 columns with name EmployeNum, EmployeName, Designation and Department Name.
  1. CREATE TABLE #tblXMLData  
  2.     (  
  3.       EmployeNum NVARCHAR(20) ,  
  4.       EmployeName NVARCHAR(50) ,  
  5.       Designation NVARCHAR(50) ,  
  6.       DepName NVARCHAR(50)  
  7.     )  
CREATE STORED PROCEDURE
 
Now, let's create a procedure for inserting XML Data into #tblXMLData Temp Table.
  1. ALTER PROCEDURE [dbo].[InsertXMLData] ( @XMLdata AS XML )  
  2. AS   
  3.     BEGIN   
  4.         DECLARE @XML NVARCHAR(2000) ,  
  5.             @count INT ;   
  6.         WITH XMLNAMESPACES ('urn' AS pd)   
  7.         SELECT  @count = @XMLdata.exist('(//pd:EMPNAME)')   
  8.         SET @XML = 'WITH XMLNAMESPACES (''urn'as pd)   
  9. INSERT INTO #tblXMLData (EmployeNum,EmployeName, Designation,DepName)   
  10. SELECT   
  11. n.c.value(''(pd:EMPNO/text())[1]'',''nvarchar(20)'')AS EmployeNum , '   
  12.         IF ( @count = 1 )   
  13.             SET @XML += 'n.c.value(''(pd:EMPNAME/text())[1]'',''nvarchar(50)'')AS EmployeName,'   
  14.         ELSE   
  15.             SET @XML += 'NULL,'   
  16.         SET @XML += 'ca.c.value(''(pd:DESG/text())[1]'',''nvarchar(50)'') AS Designation,   
  17. ca.c.value(''(pd:DEPNAME/text())[1]'',''nvarchar(50)''AS DepName   
  18. FROM @XMLdata.nodes(''/pd:SampleXML/pd:Employee'')x(t)   
  19. CROSS APPLY x.t.nodes(''pd:EmployeeDetails'') n(c)   
  20. CROSS APPLY x.t.nodes(''pd:Department'') ca(c)'   
  21.   
  22.         --DECLARE @XMLID NVARCHAR(2000)   
  23.   --      SET @XML += 'SET @XMLID =SCOPE_IDENTITY();'   
  24.   
  25.         DECLARE @data NVARCHAR(2000)   
  26.         SET @data = '@XMLdata XML'   
  27.   
  28.         EXEC sp_Executesql @XML, @data, @XMLdata   
  29.     END  
EXECUTE PTOCEDURE AND INSERT XML DATA
 
Now, it’s time to execute the created procedure to insert XML data into the created table.
  1. EXEC [InsertXMLData] '   <SampleXML xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="urn">  
  2.   <Employee>  
  3.     <EmployeeDetails>  
  4.         <EMPNO>001</EMPNO>  
  5.         <EMPNAME>NIKUNJ SATASIYA</EMPNAME>  
  6.     </EmployeeDetails>  
  7.     <Department>  
  8.         <DESG>SOFTWARE ENGINEER</DESG>  
  9.         <DEPNAME>SOFTWARE DEVELOPMENT</DEPNAME>  
  10.     </Department>  
  11.   </Employee>  
  12.     <Employee>  
  13.     <EmployeeDetails>  
  14.         <EMPNO>002</EMPNO>  
  15.         <EMPNAME>HIREN DOBARIYA</EMPNAME>  
  16.     </EmployeeDetails>  
  17.     <Department>  
  18.         <DESG>SOFTWARE ENGINEER</DESG>  
  19.         <DEPNAME>SOFTWARE DEVELOPMENT</DEPNAME>  
  20.     </Department>  
  21.   </Employee>  
  22.   <Employee>  
  23.     <EmployeeDetails>  
  24.         <EMPNO>003</EMPNO>  
  25.         <EMPNAME>VIVEK GHADIYA</EMPNAME>  
  26.     </EmployeeDetails>  
  27.     <Department>  
  28.         <DESG>SOFTWARE ENGINEER</DESG>  
  29.         <DEPNAME>SOFTWARE DEVELOPMENT</DEPNAME>  
  30.     </Department>  
  31.   </Employee>  
  32.   <Employee>  
  33.     <EmployeeDetails>  
  34.         <EMPNO>004</EMPNO>  
  35.         <EMPNAME>PRATIK PANSURIYA</EMPNAME>  
  36.     </EmployeeDetails>  
  37.     <Department>  
  38.         <DESG>SOFTWARE ENGINEER</DESG>  
  39.         <DEPNAME>SOFTWARE DEVELOPMENT</DEPNAME>  
  40.     </Department>  
  41.   </Employee>  
  42.   <Employee>  
  43.     <EmployeeDetails>  
  44.         <EMPNO>005</EMPNO>  
  45.         <EMPNAME>SHREYA PATEL</EMPNAME>  
  46.     </EmployeeDetails>  
  47.     <Department>  
  48.         <DESG>HR</DESG>  
  49.         <DEPNAME>HR DEVELOPMENT</DEPNAME>  
  50.     </Department>  
  51.   </Employee>  
  52. </SampleXML > '  
SELECT DATA
 
Now, I just write Select Statement to get all the Inserted Records.
  1. SELECT * FROM  #tblXMLData  
DROP TEMP TABLE
 
After I have finished my insertion and selection operation, I just drop my created temp table.
  1. DROP TABLE #tblXMLData  
Output
 
 
 
Summary
 
In this article, I explained how to insert XML data into the table using SQL server. I hope it helps you to achieve your requirements.

Codingvila
Codingvila is an educational website, developed to help tech specialists/beginners.