Best way to bulk load XML data into a SQL Server 2005 database

I need to drop about 25 - 30 XML into my SQL Server 2005 database (total size will be about 10 MB). And I need this logic to start automatically as soon as new xml files are copied to the server.

I read a lot of posts on this site, as well as on other sites, but I can’t conclude what I should use to destroy data.

Pls tell me which option should i go with

  • SqlBulk Copy
  • C # deserialization
  • SSIS

I need to create C # classes for my data models. So C # deserialization was my first choice. But PLS will tell me which option will be right in terms of performance.

Another thing I forgot to mention is the structure of the XML files. That would not be so. I will have tables in which there will be all the columns that can be filled. But xmls will not have all the data at any time.

Xml sample

<?xml version="1.0" encoding="utf-8"?>
<estateList date="2012-08-06T12:17:05">
  <uniqueID>22XXln</uniqueID>
  <category name="Apartment" /> 
  <listingAgent>
     <name>DIW Office</name>  
     <telephone type="BH">96232 2345</telephone> 
     <telephone type="BH">9234 2399</telephone>
     <email>abcd@abc.com</email>    
  </listingAgent>
  <inspectionTimes /> 
  <description>AVAILABLE NOW. </description> 
  <price>0</price>  
  <address display="yes">      
    <street>Lachlsan Street</street>        
    <ImagesContainer>        
       <img id="m" modTime="2012-08-06-12:17:05" url="http://images/2409802.jpg" format="jpg" /> 
       <img id="a" modTime="2012-08-06-12:17:05" /> 
    </ImagesContainer>     
  </address>
</estateList>

Thank.

+5
source share
1 answer

Given that you have XML in an SQL variable, you can easily parse most of the information using direct T-SQL with XQuery support added in SQL Server 2005.

Try something like:

DECLARE @Input XML = '<estateList date="2012-08-06T12:17:05">
  <uniqueID>22XXln</uniqueID>
  <category name="Apartment" /> 
  <listingAgent>
     <name>DIW Office</name>  
     <telephone type="BH">96232 2345</telephone> 
     <telephone type="BH">9234 2399</telephone>
     <email>abcd@abc.com</email>    
  </listingAgent>
  <inspectionTimes /> 
  <description>AVAILABLE NOW. </description> 
  <price>0</price>  
  <address display="yes">      
    <street>Lachlsan Street</street>        
    <ImagesContainer>        
       <img id="m" modTime="2012-08-06-12:17:05" url="http://images/2409802.jpg" format="jpg" /> 
       <img id="a" modTime="2012-08-06-12:17:05" /> 
    </ImagesContainer>     
  </address>
</estateList>'

SELECT
    EstateListDate = EstL.value('@date', 'datetime'),
    UniqueID = EstL.value('(uniqueID)[1]', 'varchar(20)'),
    Category = EstL.value('(category/@name)[1]', 'varchar(20)'),
    ListingAgentName = EstL.value('(listingAgent/name)[1]', 'varchar(50)'),
    ListingAgentTel = EstL.value('(listingAgent/telephone)[1]', 'varchar(50)'),
    ListingAgentEMail = EstL.value('(listingAgent/email)[1]', 'varchar(250)'),
    [Description] = EstL.value('(description)[1]', 'varchar(250)'),
    Price = EstL.value('(price)[1]', 'decimal(14,2)'),
    DisplayAddress = EstL.value('(address/@display)[1]', 'varchar(10)'),
    AddressStreet = EstL.value('(address/street)[1]', 'varchar(100)')
FROM @input.nodes('/estateList') AS Tbl(EstL)

and you should get:

enter image description here

This data can be easily inserted into the table. And this query can be run with any number of XML files on the disk using a fairly simple SSIS package (list XML, load each into an SQL variable, parse it, insert data into tables, etc.).

: :

  • ? : ?
  • ?

.....

: UniqueID <img> XML- ( ):

SELECT
    UniqueID = @input.value('(/estateList/uniqueID)[1]', 'varchar(20)'),
    ImageID = Images.value('(img/@id)[1]', 'varchar(20)'),
    ImageModTime = Images.value('(img/@modTime)[1]', 'varchar(50)'),
    ImageFormat = Images.value('(img/@format)[1]', 'varchar(20)'),
    ImageURL = Images.value('(img/@url)[1]', 'varchar(250)')
FROM 
    @input.nodes('/estateList/address/ImagesContainer') AS Tbl(Images)
+4

All Articles