Try this:

DECLARE @input XML = '<dataset> 
 <metadata>
  <item name="NAME_LAST" type="xs:string" length="62" /> 
  <item name="NAME_FIRST" type="xs:string" length="62" /> 
  <item name="NAME_MIDDLE" type="xs:string" length="32" />
 </metadata>
<data>
<row>
 <value>SMITH</value> 
 <value>MARY</value> 
 <value>N</value> 
</row>
<row>
 <value>SMITH2</value> 
 <value>MARY2</value> 
 <value>N2</value> 
</row>
</data>
</dataset>'

INSERT INTO dbo.YourTable(ColName, ColFirstName, ColOther)
   SELECT
      Name = XCol.value('(value)[1]','varchar(25)'),
      FirstName = XCol.value('(value)[2]','varchar(25)'),
      OtherValue = XCol.value('(value)[3]','varchar(25)')
   FROM 
      @input.nodes('/dataset/data/row') AS XTbl(XCol)
Answer from marc_s on Stack Overflow
Top answer
1 of 2
33

Try this:

DECLARE @input XML = '<dataset> 
 <metadata>
  <item name="NAME_LAST" type="xs:string" length="62" /> 
  <item name="NAME_FIRST" type="xs:string" length="62" /> 
  <item name="NAME_MIDDLE" type="xs:string" length="32" />
 </metadata>
<data>
<row>
 <value>SMITH</value> 
 <value>MARY</value> 
 <value>N</value> 
</row>
<row>
 <value>SMITH2</value> 
 <value>MARY2</value> 
 <value>N2</value> 
</row>
</data>
</dataset>'

INSERT INTO dbo.YourTable(ColName, ColFirstName, ColOther)
   SELECT
      Name = XCol.value('(value)[1]','varchar(25)'),
      FirstName = XCol.value('(value)[2]','varchar(25)'),
      OtherValue = XCol.value('(value)[3]','varchar(25)')
   FROM 
      @input.nodes('/dataset/data/row') AS XTbl(XCol)
2 of 2
9
Insert XML Data into sql Server table

Declare @retValue1 varchar(50);
Declare @XmlStr XML;
SET @XmlStr='<Customers>
 <customer>
    <ID>111589</ID>
    <FirstName>name1</FirstName>
    <LastName>Lname1</LastName>
    <Company>ABC</Company>
  </customer>
  <customer>
    <ID>12345</ID>
    <FirstName>name2</FirstName>
    <LastName>Lname2</LastName>
    <Company>ABC</Company>
  </customer>
  <customer>
    <ID>14567</ID>
    <FirstName>name3</FirstName>
    <LastName>Lname3</LastName>
    <Company>DEF</Company>
  </customer>
</Customers>';

@retValue='Failed';
 
INSERT INTO  test_xmlinsert
SELECT
COALESCE([Table].[Column].value('ID[1]', 'int'),0) as 'ID',
[Table].[Column].value('FirstName [1]', 'varchar(50)') as ' FirstName ',
[Table].[Column].value(' LastName[1]', 'varchar(50)') as ' LastName',
[Table].[Column].value(' Company [1]', 'varchar(50)') as ' Company'
 FROM @XmlStr.nodes('/ Customers / customer') as Table
IF(@@ROWCOUNT > 0 )
  SET @retValue='SUCCESS';
๐ŸŒ
SQLServerCentral
sqlservercentral.com โ€บ forums โ€บ topic โ€บ insert-xml-into-sql-table
Insert XML into SQL Table โ€“ SQLServerCentral Forums
June 5, 2012 - Thanks for your help in advance again. All you need to do is add the columns you want to the query I wrote. Copy-and-paste and a tiny bit of typing. That should give you what you need on the XML query. - Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, ...
Discussions

sql server - Insert XML File into specific sql table - Database Administrators Stack Exchange
Check the number of columns being returned by your SELECT * I would suggest that it is rarely a good idea to use SELECT * when trying to insert into another table. You should always explicitly specify the selected columns. I played around with your example XML data by putting it in a file and ... More on dba.stackexchange.com
๐ŸŒ dba.stackexchange.com
INSERT XML into SQL Server 2008 database - Stack Overflow
Hello I'm trying to insert some XML data into a table on SQL Server 2008. However I keep getting thrown this error; XML parsing: line 1, character 39, unable to switch the encoding ยท The database column filemeta uses the XML datatype, and I've switch the encoding to UTF-16 which I believe ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
Importing an XML file into a sql table โ€“ SQLServerCentral Forums
Importing an XML file into a sql table Forum โ€“ Learn more on SQLServerCentral More on sqlservercentral.com
๐ŸŒ sqlservercentral.com
February 17, 2019
XML data into SQL Tables
Hi, I have a xml in C# code and has to store in sql table (4 tables). what is best way to insert xml records in tables send XML string to Stored procedure and there we store xml data into multiple tables? using SQL bulk copy in c# and store itโ€ฆ More on learn.microsoft.com
๐ŸŒ learn.microsoft.com
3
0
February 24, 2021
๐ŸŒ
T-SQL Tutorial
tsql.info โ€บ xml โ€บ insert-xml-into-sql-table.php
Insert XML into SQL table in SQL Server - T-SQL Tutorial
Create a table in SQL Server that has a column of data type XML to store the XML data. For example, let's create a table called MyXmlTable with a single column called XmlData of type XML: ... Prepare the XML data that you want to insert into the table. You can either write the XML data directly in your SQL query, or you can load it from a file or a variable in your programming language.
๐ŸŒ
MSSQLTips
mssqltips.com โ€บ home โ€บ simple way to import xml data into sql server with t-sql
Simple way to Import XML Data into SQL Server with T-SQL
February 25, 2022 - The FROM clause comes from using the OPENROWSET operation using the BULK option and the SINGLE_BLOB option to have the data returned from the XML file into a single column and row. The function nodes(), along with CROSS APPLY, allows navigation through the XML elements to get all of the Customer objects properly encapsulated. After the insert, you can query the table to check the results.
๐ŸŒ
Microsoft Learn
learn.microsoft.com โ€บ en-us โ€บ sql โ€บ t-sql โ€บ xml โ€บ insert-xml-dml
insert (XML DML) - SQL Server | Microsoft Learn
insert Expression1 ( {as first | as last} into | after | before Expression2 ) Expression1 Identifies one or more nodes to insert. This can be a constant XML instance; a reference to a typed XML data type instance of the same XML Schema collection on which the modify method is being applied; an untyped XML data type instance using a stand-alone sql:column()/sql:variable() function; or an XQuery expression.
Find elsewhere
๐ŸŒ
Sonra
sonra.io โ€บ home โ€บ xml โ€บ how to insert xml into sql tables (2025 guide)
How to Insert XML into SQL Tables (2026 Guide) - Sonra
December 5, 2025 - BULK INSERT or OPENROWSET for SQL Server. These tools handle the raw movement of your XML content into the database, making it ready for storage or further processing. ... Once loaded, storing XML means writing that XML content into a column inside your tables.
๐ŸŒ
MSSQLTips
mssqltips.com โ€บ home โ€บ importing and processing data from xml files into sql server tables
Importing and Processing data from XML files into SQL Server tables
February 24, 2022 - In the script below, I am first creating a table with a column of data type XML and then reading the XML data from the file using the OPENROWSET function by specifying the file location and name of the XML file as you can see below: CREATE DATABASE OPENXMLTesting GO USE OPENXMLTesting GO 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 'D:\OpenXMLTesting.xml', SINGLE_BLOB) AS x; SELECT * FROM XMLwithOpenXML
๐ŸŒ
SQLServerCentral
sqlservercentral.com โ€บ forums โ€บ topic โ€บ importing-an-xml-file-into-a-sql-table
Importing an XML file into a sql table โ€“ SQLServerCentral Forums
February 17, 2019 - :2 :Toshiba : TOSHIBA :2 :Toshiba : TOSHANDH :2 :Toshiba : TOSCNOTE --------------------------------------------------- However with the below sql it returns results like this. --------------------------------------- :supplier_id: name : Symbol : :1 :HP : COMPAQ: :2 :Toshiba : TOSHIBA --------------------------------------------------- ie it returns the first <Symbol> entry but does not repeat. this is what i am trying to achieve but as im new to XML its hardd putting this into words. ... INSERT INTO XMLwithOpenXML(XMLData, LoadedDateTime) SELECT CONVERT(XML, BulkColumn) AS BulkColumn, GETDATE() FROM OPENROWSET(BULK 'C:\Scripts\icecat\supplier_mapping.xml', SINGLE_BLOB) AS x;
๐ŸŒ
Techfunda
techfunda.com โ€บ howto โ€บ 205 โ€บ insert-records-from-xml-to-sql-server-database-table
Insert records from XML to SQL Server database table in SQL Server - Tech Funda
DECLARE @xmlData Xml SET @xmlData = '<PersonalDetails> <PersonalDetail> <FirstName>XmlFirstName 11</FirstName> <LastName>XmlLastName 1</LastName> <Age>30</Age> <Active>1</Active> </PersonalDetail> <PersonalDetail> <FirstName>XmlFirstName 22</FirstName> <LastName>XmlLastName 2</LastName> <Age>25</Age> <Active>1</Active> </PersonalDetail> </PersonalDetails>' EXEC InsertPersonalDetailsFromXML @xmlData
๐ŸŒ
Microsoft Learn
learn.microsoft.com โ€บ en-us โ€บ answers โ€บ questions โ€บ 286730 โ€บ xml-data-into-sql-tables
XML data into SQL Tables - Microsoft Q&A
February 24, 2021 - In general: The best way is to handle it entirely in your .NET application. Cause not every XML construct is easily transferred to a set of relations. 1) Only viable when having a concise and not too complex XSD. 2) Bulk copy is only necessary when operating with large data chunks. Use staging tables for the data to insert.
๐ŸŒ
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, result.[Address], result.City, result.Region, result.PostalCode, result.Country, result.HomePhone, result.Extension, result.Photo, result.Notes, result.ReportsTo, result.PhotoPath FROM @xml CROSS APPLY ( SELECT LastName = z.value('LastName[1]', 'nvarchar(20)'), FirstName = z.value('FirstName[1]', 'nvarchar(10)'), Title = z.value('Title[1]', 'nvarchar
๐ŸŒ
Devart
devart.com โ€บ dbforge โ€บ sql โ€บ data-pump โ€บ import-from-xml-to-sql-server.html
Import XML File into SQL Server - using Data Pump
Finally, to clear the mapping of all columns, click Clear Mappings on the toolbar. To restore it, click Fill Mapping. 7. On the Modes page, select an import mode. If you import data into an existing table, all the options will be available. If you select a new table as a target data source, only the Append mode can be used. ... Optionally, you can select the Use a single transaction and Use bulk insert checkboxes, the latter of which reduces the quantity of statements and speeds up import but can affect the error handling mechanism.
Top answer
1 of 3
4

Full query as you want:

DECLARE @XmlData XML
Set @XmlData = '<vehicles> 
  <vehicle>
    <vehiclereg>AB12CBE</vehiclereg>
    <anotherprop>BLAH</anotherprop>
  </vehicle>
  <vehicle>
    <vehiclereg>AB12CBE</vehiclereg>
    <anotherprop>BLAH</anotherprop>
  </vehicle>
</vehicles>'
SELECT T.Vehicle.value('./vehiclereg[1]', 'NVARCHAR(10)') AS vehiclereg,
       T.Vehicle.query('.'),
       GETDATE()
FROM @XmlData.nodes('/vehicles/vehicle') AS T(Vehicle)

2 of 3
4

Just need to remember XML is case sensitive. You had:

FROM @XmlData.nodes('Vehicles/Vehicle') AS T(Vehicle)

but you should have had:

FROM @XmlData.nodes('/vehicles/vehicle') AS T(Vehicle)

Also as TT pointed out there was no column named Registration

This should do it:

DECLARE @XmlData XML
Set @XmlData = '<vehicles> 
  <vehicle>
    <vehiclereg>AB12CBE</vehiclereg>
    <anotherprop>BLAH</anotherprop>
  </vehicle>
  <vehicle>
    <vehiclereg>AB12CBE</vehiclereg>
    <anotherprop>BLAH</anotherprop>
  </vehicle>
</vehicles>'
SELECT  Vehicle.value('(vehiclereg)[1]', 'NVARCHAR(10)') AS vehiclereg,
        Vehicle.value('.', 'NVARCHAR(MAX)'),
       GETDATE()
FROM @XmlData.nodes('/vehicles/vehicle') AS T(Vehicle)

Result:

This would return XML:

DECLARE @XmlData XML
Set @XmlData = '<vehicles> 
  <vehicle>
    <vehiclereg>AB12CBE</vehiclereg>
    <anotherprop>BLAH</anotherprop>
  </vehicle>
  <vehicle>
    <vehiclereg>AB12CBE</vehiclereg>
    <anotherprop>BLAH</anotherprop>
  </vehicle>
</vehicles>'
SELECT T.Vehicle.value('(vehiclereg)[1]', 'NVARCHAR(10)') AS vehiclereg,
       T.Vehicle.query('.'),
       GETDATE()
FROM @XmlData.nodes('vehicles/vehicle') AS T(Vehicle)

Result:

๐ŸŒ
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
You can also explicitly specify the column names as follows: 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 ...
๐ŸŒ
SQL Shack
sqlshack.com โ€บ working-with-xml-data-in-sql-server
Working with XML Data in SQL Server
October 11, 2019 - The simplest way to convert data from SQL tables into XML format is to use the FOR XML AUTO and FOR XML PATH clauses. The FOR XML AUTO clause converts each column in the SQL table into an attribute in the corresponding XML document.
๐ŸŒ
IBM
ibm.com โ€บ docs โ€บ en โ€บ db2 โ€บ 11.1.0
Inserting XML data
Before you can insert XML documents, you must create a table that contains an XML column, or add an XML column to an existing table.
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 3413025 โ€บ insert-xml-data-into-sql-server-table
Insert XML data into SQL Server table - Stack Overflow
DECLARE @count INT DECLARE @id INT SET @count = 1 SET @id = totalNumberOfRecordsInTempTable -- Get records from xml to temp table first WHILE @count <= @id BEGIN INSERT INTO YourTable (Column1, Column2, ...) SELECT Column1, Column2, ...