SQL Server is capable of reading XML and inserting it as you need. Here is an example of an XML file and insertion pulled from here:

XML:

<Products>
  <Product>
    <SKU>1</SKU>
    <Desc>Book</Desc>
  </Product>
  <Product>
    <SKU>2</SKU>
    <Desc>DVD</Desc>
  </Product>
  <Product>
    <SKU>3</SKU>
    <Desc>Video</Desc>
  </Product>
</Products>

Insert statement that is parsing the XML:

INSERT INTO Products (sku, product_desc) 
SELECT X.product.query('SKU').value('.', 'INT'),
       X.product.query('Desc').value('.', 'VARCHAR(30)')
FROM ( 
SELECT CAST(x AS XML)
FROM OPENROWSET(
     BULK 'C:\Products.xml',
     SINGLE_BLOB) AS T(x)
     ) AS T(x)
CROSS APPLY x.nodes('Products/Product') AS X(product);
Answer from Ocelot20 on Stack Overflow
Top answer
1 of 2
13

SQL Server is capable of reading XML and inserting it as you need. Here is an example of an XML file and insertion pulled from here:

XML:

<Products>
  <Product>
    <SKU>1</SKU>
    <Desc>Book</Desc>
  </Product>
  <Product>
    <SKU>2</SKU>
    <Desc>DVD</Desc>
  </Product>
  <Product>
    <SKU>3</SKU>
    <Desc>Video</Desc>
  </Product>
</Products>

Insert statement that is parsing the XML:

INSERT INTO Products (sku, product_desc) 
SELECT X.product.query('SKU').value('.', 'INT'),
       X.product.query('Desc').value('.', 'VARCHAR(30)')
FROM ( 
SELECT CAST(x AS XML)
FROM OPENROWSET(
     BULK 'C:\Products.xml',
     SINGLE_BLOB) AS T(x)
     ) AS T(x)
CROSS APPLY x.nodes('Products/Product') AS X(product);
2 of 2
6

I tried this and for 975 rows from a 1MB XML file, this took about 2.5 minutes to execute on a very fast PC.

I switched to using OpenXml in a multi-step process and process takes less than a second.

CREATE TABLE XMLwithOpenXML
(
    Id INT IDENTITY PRIMARY KEY,
    XMLData XML,
    LoadedDateTime DATETIME
)    

INSERT INTO XMLwithOpenXML(XMLData, LoadedDateTime)
SELECT CONVERT(XML, BulkColumn) AS BulkColumn, GETDATE() 
FROM OPENROWSET(BULK 'clients.xml', SINGLE_BLOB) AS x;



DECLARE @XML AS XML, @hDoc AS INT, @SQL NVARCHAR (MAX)    

SELECT @XML = XMLData FROM XMLwithOpenXML WHERE ID = '1' -- The row to process    

EXEC sp_xml_preparedocument @hDoc OUTPUT, @XML


INSERT INTO Clients
SELECT CustomerID, CustomerName
FROM OPENXML(@hDoc, 'Clients/Client')
WITH 
(
    CustomerID varchar 'ID',
    CustomerName varchar 'Name'
)


EXEC sp_xml_removedocument @hDoc
GO

I got this from here: http://www.mssqltips.com/sqlservertip/2899/importing-and-processing-data-from-xml-files-into-sql-server-tables/

Basically you load the XML into a table as a big blob of text, then you use OpenXml to process it.

🌐
C# Corner
c-sharpcorner.com › article › how-to-insert-bulk-records-into-sql-server-using-xml-data-type
How To Insert Bulk Records Into SQL Server Using XML Data Type
March 10, 2018 - ... Create user defined type in database and send your whole table to this type. SQL query can read every record and perform insert action to the database. Using XML data type, pass XML to the database.
Discussions

Bulk Insert XML document into SQL table. How? – SQLServerCentral Forums
IF you get your "rowterminator" right you can probably use the BULK INSERT into a blob but in that case I would rather use the above snippet. ... That's not what I'm looking for. What I'm looking for is to transfer XML data into SQL table rowsets: More on sqlservercentral.com
🌐 sqlservercentral.com
December 9, 2008
Inserting data into SQL SERVER table from a bulk #xml - Stack Overflow
I am trying to insert data from external xml using a stored procedure Though my SP is working but it is not inserting all the records. here is my table CREATE TABLE MFR ( MESSAGEID varchar(... More on stackoverflow.com
🌐 stackoverflow.com
Looking for a good Bulk Insert XML Shredding example for SQL 2005 - Stack Overflow
A little help needed. I'm receiving an xml file similar to this: More on stackoverflow.com
🌐 stackoverflow.com
Bulk insert XML data into SQL Server - Stack Overflow
This option offers a higher performance ... of SQL Server to another using a data file. It does not prompt for each field. Use this option when you are transferring data that contains ANSI extended characters and you want to take advantage of the performance of native mode. ... Find the answer to your question by asking. Ask question ... See similar questions with these tags. ... 12 Why do I get "XML parsing: line 2, character 0, incorrect document syntax" when Bulk Inserting in MS SQL ... More on stackoverflow.com
🌐 stackoverflow.com
February 15, 2010
🌐
Medium
joemoceri.medium.com › bulk-insert-xml-into-sql-server-table-ca2653ab1806
Bulk insert XML into SQL Server table | by Joe Moceri | Medium
July 28, 2019 - DECLARE @xml TABLE (x XML) INSERT @xml SELECT x FROM OPENROWSET(BULK 'EmployeesData.xml', SINGLE_BLOB) AS T(x) INSERT INTO Employees SELECT result.LastName, result.FirstName, result.Title, result.TitleOfCourtesy, result.BirthDate, result.HireDate, ...
🌐
C# Corner
c-sharpcorner.com › article › bulk-insert-update-xml-data-into-sql-table
Bulk Insert, Update XML Data Into SQL Table
September 15, 2018 - Now user would like to update old data & if the record does not into a table then insert that record. Here I am using @EmployeeXML_Old XML to insert data which contains some records and @EmployeeXML contains updated data as well as new data, I am using match condition to update the data as well as insert the data. ... Summary In this article, we learned Bulk Insert, Update XML data into SQL Table.
🌐
GitHub
github.com › MicrosoftDocs › sql-docs › blob › live › docs › relational-databases › import-export › examples-of-bulk-import-and-export-of-xml-documents-sql-server.md
sql-docs/docs/relational-databases/import-export/examples-of-bulk-import-and-export-of-xml-documents-sql-server.md at live · MicrosoftDocs/sql-docs
INSERT INTO T ( XmlCol ) SELECT x.BulkColumn FROM OPENROWSET( BULK 'C:\SampleFolder\SampleData3.txt', SINGLE_BLOB) AS x; By using SINGLE_BLOB in this case, you can avoid a mismatch between the encoding of the XML document (as specified by the ...
Author   MicrosoftDocs
🌐
SQLServerCentral
sqlservercentral.com › forums › topic › bulk-insert-xml-document-into-sql-table-how
Bulk Insert XML document into SQL table. How? – SQLServerCentral Forums
December 9, 2008 - IF you get your "rowterminator" right you can probably use the BULK INSERT into a blob but in that case I would rather use the above snippet. ... That's not what I'm looking for. What I'm looking for is to transfer XML data into SQL table rowsets:
🌐
Microsoft Learn
learn.microsoft.com › en-us › sql › relational-databases › import-export › examples-of-bulk-import-and-export-of-xml-documents-sql-server
Bulk import & export of XML documents - SQL Server | Microsoft Learn
INSERT INTO T ( XmlCol ) SELECT x.BulkColumn FROM OPENROWSET( BULK 'C:\SampleFolder\SampleData3.txt', SINGLE_BLOB) AS x; By using SINGLE_BLOB in this case, you can avoid a mismatch between the encoding of the XML document (as specified by the ...
Find elsewhere
🌐
C# Corner
c-sharpcorner.com › blogs › bulk-insert-into-sql-database-using-xml-file1
Bulk insert into sql database using xml file
October 14, 2011 - It makes no difference other than limitation on the size of the document you wish to pass through Write the following code in button click event. string connStr = ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString; DataSet ds = new DataSet(); ds.ReadXml(Server.MapPath("XML_Files/Employee.xml")); string strxml = XDocument.Load(Server.MapPath("XML_Files/Employee.xml")).ToString(); SqlConnection sqlconn = new SqlConnection(connStr); SqlCommand sqlcmd = new SqlCommand(); sqlcmd.Connection = sqlconn; sqlcmd.CommandType = CommandType.StoredProcedure; sqlcmd.CommandText = "xmlToEmp"; sqlcmd.Parameters.AddWithValue("@xmlstr", strxml); sqlconn.Open(); sqlcmd.ExecuteNonQuery(); sqlconn.Close();
🌐
Microsoft Learn
learn.microsoft.com › en-us › sql › relational-databases › sqlxml-annotated-xsd-schemas-xpath-queries › bulk-load-xml › introduction-to-xml-bulk-load-sqlxml-4-0
Introduction to XML Bulk Load (SQLXML) - SQL Server | Microsoft Learn
March 3, 2023 - Then, a Transact-SQL BULK INSERT statement retrieves these records from the files and stores them in the corresponding tables. You can specify the location for these temporary files by using the TempFilePath property.
🌐
O'Reilly
oreilly.com › library › view › ado-net-3-5-cookbook › 9780596101404 › ch10s10.html
10.9. XML Bulk Loading with SQL Server - ADO.NET 3.5 Cookbook, 2nd Edition [Book]
The solution uses an XML schema file that describes the XML data file. The schema file is named Customers.xsd and is shown in Example 10-14. The solution expects this file to be in the same directory as the solution file BulkLoad.sln. Example 10-14. File: Customers.xsd · <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:sql="urn:schemas-microsoft-com:mapping-schema"> <xsd:element name="ROOT" sql:is-constant="true"> <xsd:complexType> <xsd:sequence> <xsd:element ref="Customers" /> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:element name="Customers" sql:relation="Customers"> <xsd:complexType> <xsd:sequence> <xsd:element name="CustomerID" type="xsd:string" sql:datatype="nvarchar(5)" /> <xsd:element ...
🌐
Microsoft Knowledge Base
mskb.pkisolutions.com › kb › 316005
How to import XML into SQL Server with the XML Bulk Load ...
Run the VBScript program C:\Insertcustomers.vbs to insert the three customer records into the Customer table. In SQL Query Analyzer, switch to the MyDatabase database, and then run this query: SELECT * FROM Customer Note that the three records created in the "Create the XML data source file" heading are now in the Customer table. ... Mapping an XML document to multiples tables by using a relationship specified in the XML schema file. Generating table schemas before bulk loading.
🌐
Csvreader
csvreader.com › code › cs › bulk_insert_xml.php
Bulk Insert XML File in C# - CSV Reader
Code sample in C# for bulk inserting XML file using the DataStreams framework.
🌐
Stack Overflow
stackoverflow.com › questions › 27223232 › inserting-data-into-sql-server-table-from-a-bulk-xml
Inserting data into SQL SERVER table from a bulk #xml - Stack Overflow
IF EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID('SP_ADDREC_MFR')) DROP PROCEDURE SP_ADDREC_MFR GO CREATE PROCEDURE SP_ADDREC_MFR ( @BR CHAR(2),@xmlString XML ) AS BEGIN SET NOCOUNT ON; SET CONCAT_NULL_YIELDS_NULL ON; DECLARE @xmlTag varchar(100) IF CHARINDEX('</mismatchedFields>', CAST(@xmlString as varchar(MAX))) > 0 SET @xmlTag = 'mismatchedFields' IF CHARINDEX('</InterMismatchedFields>', CAST(@xmlString as varchar(MAX))) > 0 SET @xmlTag = 'InterMismatchedFields' INSERT INTO MFR (MESSAGEID,REPORTINGTYPE,SENTBY, SENTTO, CREATIONTIMESTAMP , TRADEID, PARTYIDTYPE,PARTYID, COUNTERPARTYID
Top answer
1 of 2
2

The basic syntax for using bcp is:

bcp <table_name> <direction> <file_name> <options> 

Where the arguments take the following values:

  • table_name is the fully qualified name of the table. For example, you might use inventory.dbo.fruits to insert records into the fruits table, owned by the database owner, in the inventory database.
  • direction indicates whether you want to import (“in” direction) or export (“out” direction) data.
  • file_name is the full path to the file. For example, you could import the file C:\fruit\inventory.txt.
  • options allow you to specify parameters for the bulk operation. For example, you can specify the maximum number of errors allowed with the –m option. You may also use the –x option to specify an XML file format. Consult Microsoft’s bcp documentation for a full list.

Will need more info to know what switches to use, but it should be somehting like

bcp database.dbo.MyTable in "C:\folder\xmlfile.xml" -c -T

-c Performs the operation using a character data type.
-T Specifies that the bcp utility connects to SQL Server with a trusted connection using integrated security.

Also here is Microsoft's bcp Utility which should help you with knowing what switches to use.

2 of 2
0

use -N switch if your file contains unicode characters.

-N : Performs the bulk-copy operation using the native (database) data types of the data for noncharacter data, and Unicode characters for character data. This option offers a higher performance alternative to the -w option, and is intended for transferring data from one instance of SQL Server to another using a data file. It does not prompt for each field. Use this option when you are transferring data that contains ANSI extended characters and you want to take advantage of the performance of native mode.

🌐
Microsoft Learn
learn.microsoft.com › en-us › sql › relational-databases › import-export › xml-format-files-sql-server
XML Format Files (SQL Server) - SQL Server | Microsoft Learn
July 15, 2025 - XML format files must adhere to this schema, which is defined in the XML Schema Definition Language (XSDL). XML format files are only supported when SQL Server tools are installed together with SQL Server Native Client. You can use an XML format file with a bcp command, BULK INSERT statement, or INSERT ...
🌐
SQLServerCentral
sqlservercentral.com › forums › topic › bulk-insert-export-using-xml
Bulk Insert / Export using XML – SQLServerCentral Forums
September 22, 2007 - It's really no different than your normal SQL Server licencing. IE: You either need to have a processor or per seat license. Snapshot Replication would be the best way to carry out what you are talking about I think so long as your server is correctly licensed. If you are trying to create your own replication engine I would simply use BCP/DTS and flat files. The problem with using XML is that you bloat the file size that needs to be transfered from one machine to the other.
Top answer
1 of 1
3

Based on your XML, you could use this XQuery SELECT to extract your items from the XML:

select
    @input.value('(/ArrayOfInfringementEntity/InfringementEntity/infringementNumber)[1]', 'VARCHAR(10)') 'InfringementNumber',
    @input.value('(/ArrayOfInfringementEntity/InfringementEntity/issueAgency)[1]', 'VARCHAR(5)') 'Issue Agency',
    @input.value('(/ArrayOfInfringementEntity/InfringementEntity/infringementType)[1]', 'VARCHAR(5)') 'Infringement Type',
    @input.value('(/ArrayOfInfringementEntity/InfringementEntity/offenceEntity/offenceCode)[1]', 'INT') 'Offence Code',
    @input.value('(/ArrayOfInfringementEntity/InfringementEntity/vehicleEntity/vehicleClass)[1]', 'INT') 'Vehicle Class',
    @input.value('(/ArrayOfInfringementEntity/InfringementEntity/obligationNumber)[1]', 'VARCHAR(11)') 'Obligation Number'

Replace @input with the variable or column that holds your XML (I've used @input as a test bed in my tests).

The output looks like this:

InfringementNumber  Issue Agency    Infringement Type   Offence Code    Vehicle Class   Obligation Number
1234567891              017                1A              7777            2                obligation1

And of course, you can also do an INSERT INTO .... and use the output from this SELECT as the values to insert.

Update: if your XML column contains multiple entries (of /InfringementEntity inside the /ArrayOfInfringementEntity), you need to use a SELECT like this:

SELECT
    InfrEntity.value('(infringementNumber)[1]', 'VARCHAR(10)') 'InfringementNumber',
    InfrEntity.value('(issueAgency)[1]', 'VARCHAR(5)') 'Issue Agency',
    InfrEntity.value('(infringementType)[1]', 'VARCHAR(5)') 'Infringement Type',
    InfrEntity.value('(offenceEntity/offenceCode)[1]', 'INT') 'Offence Code',
    InfrEntity.value('(vehicleEntity/vehicleClass)[1]', 'INT') 'Vehicle Class',
    InfrEntity.value('(obligationNumber)[1]', 'VARCHAR(11)') 'Obligation Number'
from
    (yourXMLcolumn).nodes('/ArrayOfInfringementEntity/InfringementEntity') as ArrInfr(InfrEntity)
🌐
Stack Overflow
stackoverflow.com › questions › 33074696 › t-sql-issue-with-bulk-insert-xml-format-file
sql server - T-SQL, issue with BULK INSERT / XML Format File - Stack Overflow
<?xml version="1.0"?> <BCPFORMAT xmlns="https://schemas.microsoft.com/sqlserver/2004/bulkload/format" xmlns:xsi="http://w3.org/2001/XMLSchema-instance" /> <RECORD> <FIELD ID="1" xsi:type="CharTerm" TERMINATOR=',' /> <FIELD ID="2" xsi:type="CharTerm" TERMINATOR=',' /> <FIELD ID="3" xsi:type="CharTerm" TERMINATOR=',"' /> <FIELD ID="4" xsi:type="CharTerm" TERMINATOR='",' /> <FIELD ID="5" xsi:type="CharTerm" TERMINATOR=',' /> <FIELD ID="6" xsi:type="CharTerm" TERMINATOR=',' /> <FIELD ID="7" xsi:type="CharTerm" TERMINATOR=',' /> <FIELD ID="8" xsi:type="CharTerm" TERMINATOR=',' /> <FIELD ID="9" xsi:
🌐
Microsoft Learn
learn.microsoft.com › en-us › sql › t-sql › statements › bulk-insert-transact-sql
BULK INSERT (Transact-SQL) - SQL Server | Microsoft Learn
April 17, 2025 - However, BULK INSERT can't import this data directly into t_float, because its second column, c2, uses the decimal data type. Therefore, a format file is necessary. The format file must map the scientific notation float data to the decimal format of column c2. The following format file uses the SQLFLT8 data type to map the second data field to the second column: <?xml version="1.0"?> <BCPFORMAT xmlns="http://schemas.microsoft.com/sqlserver/2004/bulkload/format" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <RECORD> <FIELD ID="1" xsi:type="CharTerm" TERMINATOR="\t" MAX_LENGTH="30" /> <FIELD ID="2" xsi:type="CharTerm" TERMINATOR="\r\n" MAX_LENGTH="30" /> </RECORD> <ROW> <COLUMN SOURCE="1" NAME="c1" xsi:type="SQLFLT8" /> <COLUMN SOURCE="2" NAME="c2" xsi:type="SQLFLT8" /> </ROW> </BCPFORMAT>