Skip to content

Instantly share code, notes, and snippets.

@justinobney
Created April 16, 2013 16:44
Show Gist options
  • Save justinobney/5397488 to your computer and use it in GitHub Desktop.
Save justinobney/5397488 to your computer and use it in GitHub Desktop.
Import date from XML into SQL
-- Thanks to: http://pratchev.blogspot.com/2008/11/import-xml-file-to-sql-table.html
INSERT INTO Equipment (Id, Location, Unit, FullDescription, Equipment, PressureRating, Quantity, ReactorMaterialOfConstruction, ReactorCapacityGallons)
SELECT X.equipment.value('@id', 'INT'),
X.equipment.query('Location').value('.', 'VARCHAR(500)'),
X.equipment.query('Unit').value('.', 'VARCHAR(50)'),
X.equipment.query('Full_description').value('.', 'VARCHAR(500)'),
X.equipment.query('Equipment').value('.', 'VARCHAR(50)'),
X.equipment.query('Pressure_Rating').value('.', 'VARCHAR(50)'),
X.equipment.query('Quantity').value('.', 'VARCHAR(50)'),
X.equipment.query('Reactor_Material_of_Construction').value('.', 'VARCHAR(500)'),
X.equipment.query('Reactor_Capacity_gallons').value('.', 'VARCHAR(50)')
FROM (
SELECT CAST(x AS XML)
FROM OPENROWSET(
BULK 'C:\Users\justin\Downloads\Albemarle Fine Chemistry Services Presentation\Albemarle Fine Chemistry Services Presentation\e-data.xml',
SINGLE_BLOB) AS T(x)
) AS T(x)
CROSS APPLY x.nodes('data/item') AS X(equipment);
--INSERT INTO Products (Id, ProductName, [Synonym], Acronym, CasNo, EinecsNo, Formula, MolecularWeight, [Status], MeltingPoint, BoilingPoint)
--SELECT X.product.query('col0').value('.', 'INT'),
-- X.product.query('col1').value('.', 'VARCHAR(200)'),
-- X.product.query('col2').value('.', 'VARCHAR(200)'),
-- X.product.query('col3').value('.', 'VARCHAR(200)'),
-- X.product.query('col4').value('.', 'VARCHAR(50)'),
-- X.product.query('col5').value('.', 'VARCHAR(50)'),
-- X.product.query('col6').value('.', 'VARCHAR(200)'),
-- X.product.query('col7').value('.', 'VARCHAR(50)'),
-- X.product.query('col8').value('.', 'VARCHAR(50)'),
-- X.product.query('col9').value('.', 'VARCHAR(200)'),
-- X.product.query('col10').value('.', 'VARCHAR(200)')
--FROM (
--SELECT CAST(x AS XML)
--FROM OPENROWSET(
-- BULK 'C:\Users\justin\Downloads\Albemarle Fine Chemistry Services Presentation\Albemarle Fine Chemistry Services Presentation\a-data.xml',
-- SINGLE_BLOB) AS T(x)
-- ) AS T(x)
--CROSS APPLY x.nodes('table/sheet1') AS X(product);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment