Getting started with XML in SQL Server – PART 2 – Import XML files into your SQL Server

In order to parse XML data you need to have it ready in your SQL server. Since you will not always want to use SSIS or the Import Wizard let’s have a look at 2 different ways to import XML data into a table using TSQL.

 

1. OPENROWSET:

Probably the most common and easiest way to load any sort of BLOB data into your SQL server is the OPENROWSET function that comes with SQL server. To utilize it you need to ensure that it is enabled. Use the following code to enable it:

Once that is taken care of you can import any XML file residing on your machine into a table. Let’s assume you have an XML file in your C:\ drive in a folder called SQL with the filename Collection.xml. The code below will insert it into a temporary table which we create on the fly:

You can now query the column XML_Data_Column and display the file you just loaded.

 

2.  OLE Automation Stored Procedures

Not always does the XML data reside on your server or local machine, often times it needs to be retrieved from the internet using various APIs out there. Even though this is a little more complicated than using OPENROWSET there is no need to worry, SQL server got you covered. Using OLE Automated Stored Procedures you will be able to retrieve your data directly with a few lines of TSQL code. Once again we need to ensure that the functionality is enabled in our SQL server, to make sure it works use the code below:

In order to make the method call we need to do a few things:

  1. we need to create an object
  2. we need to open this object
  3. we need to let the object know what exactly we want to do
  4. we need to send the request
  5. optionally we can check the response status to see if our request was successful
  6. finally we need to save the response to a table.

Now this seems to be an awful lot to do, but it allows us very conveniently to do everything straight out of SQL server without having to set up a retrieval process outside of our code, so jumping through a few extra hoops might be worth it.

Let’s have a look at an example. The code below makes an API call to the website www.boardgamegeek.com and retrieves a list of all board games I own and have actually found the time to play. Since the site usually does not send the response we are looking for in the first go – they send an acknowledgement response first – we need to put the request in a loop until we get the output we really want. The code looks like this:

You can filter out any responses that do not have a HTTPStatus of 200 so you won’t save any trash in your DB, but for this example I decided it makes sense to leave it in there.

And there you have it. 2 ways to import XML using TSQL into your database. I am sure there are other ways (using a linked server connection comes to mind *ugh*) but for now these 2 will have to do.

Feel free to leave any feedback in the comments below and look out for Part 3 where I’ll show you how to get XML into your SQL server using SSIS.

 

Getting started with XML in SQL Server

Part 1 – Parsing XML

Part 3 (not available yet) will use SSIS to achieve the same results as in Part 1 & 2

Part 4 (not available yet) will show you how to use XQuery and create XML files out of SQL Server

One comment

  • Just want to note for openrowset on a network shared file, the user account needs access to both sql server and the network share. Otherwise you have to use impersonate/delegation, which could be a pain to set up.

Leave a Reply

Your email address will not be published. Required fields are marked *