Introduction
Many times, while developing any software, our clients give us data into a bulk format or need a functionality of grid which contains many records. When the user edits multiple records from that grid, it needs to update all the records at a time into a database on the click of Submit button.
Many times, while developing any software, our clients give us data into a bulk format or need a functionality of grid which contains many records. When the user edits multiple records from that grid, it needs to update all the records at a time into a database on the click of Submit button.
In this article, we are going to learn -
- Insert single record using XML into a SQL Table
- Insert multiple records using XML into a SQL Table
- Insert / Update multiple records using XML into a SQL Table at the same time
In this article, I have created a sample XML format. Using nodes() method to shred XML into multiple rows, which propagates parts of XML documents into row sets. Here I am not going to use any stored procedure or any user interface from which user submits the data. I just demonstrated in a simple way using SQL. If you like to modify this code as per your requirement.
Working with the XML Data Type in SQL Server
The XML data type, introduced in SQL Server 2005, is a powerful construct. When used wisely, it can provide useful extensions to SQL Server. Robert Sheldon, in the first part of a series, describes how to create an index a typed XML column in a table, and discusses when you should consider using an XML Datatype
Part 1
Suppose the user is passing data from a function in XML format which contains some employee information having properties like TravelId, TravelCity etc. In below code, I am using temp table #Travel. XML contains a single record. Using SQL code insert this XML data into a temp table.
The XML data type, introduced in SQL Server 2005, is a powerful construct. When used wisely, it can provide useful extensions to SQL Server. Robert Sheldon, in the first part of a series, describes how to create an index a typed XML column in a table, and discusses when you should consider using an XML Datatype
Part 1
Suppose the user is passing data from a function in XML format which contains some employee information having properties like TravelId, TravelCity etc. In below code, I am using temp table #Travel. XML contains a single record. Using SQL code insert this XML data into a temp table.
- DECLARE @EmployeeXML XML;
- SET @EmployeeXML='<?xml version="1.0"?><EmpDetail>
- <TravelId>1</TravelId><TravelCity>Pune</TravelCity>
- </EmpDetail>';
- IF OBJECT_ID('tempdb..#Travel') is not null
- Begin
- Drop Table #Travel
- End
- CREATE TABLE #Travel(TravelId int, TravelCity varchar(100))
- INSERT INTO #Travel([TravelId],[TravelCity])
- SELECT COALESCE([Table].[Column].value('TravelId[1]', 'int'),0) as 'TravelId',
- [Table].[Column].value('TravelCity[1]', 'varchar(100)') as 'TravelCity'
- FROM @EmployeeXML.nodes('/EmpDetail') as [Table]([Column])
- SELECT * FROM #Travel
Output when we execute select command as,
Part 2
In below code XML contains two records. Using SQL code insert this XML data into a temp table.
- DECLARE @EmployeeXML XML;
- SET @EmployeeXML='<?xml version="1.0"?>
- <EmpDetails>
- <EmpDetail>
- <TravelId>1</TravelId><TravelCity>Pune</TravelCity>
- </EmpDetail>
- <EmpDetail>
- <TravelId>2</TravelId><TravelCity>Mumbai</TravelCity>
- </EmpDetail>
- </EmpDetails>';
- IF OBJECT_ID('tempdb..#Travel') is not null
- Begin
- Drop Table #Travel
- End
- CREATE TABLE #Travel(TravelId int, TravelCity varchar(100))
- INSERT INTO #Travel([TravelId],[TravelCity])
- SELECT COALESCE([Table].[Column].value('TravelId[1]', 'int'),0) as 'TravelId',
- [Table].[Column].value('TravelCity[1]', 'varchar(100)') as 'TravelCity'
- FROM @EmployeeXML.nodes('/EmpDetails/EmpDetail') as [Table]([Column])
- SELECT * FROM #Travel


Join the conversation! Your thoughts help the community grow.