Importing Excel Data Using Integration Services
April 20, 2010 in Integration Services, SQLServerPedia Syndication | 7 comments
In a previous article I’ve shown you how to import data from an Excel sheet using the OPENROWSET() function. And I concluded by stating that it’s not the best option when automating your data import.
Today I’ll repeat the Excel data import process by using SQL Server Integration Services, also known as SSIS.
I’ll be using SQL Server 2008 R2, but I’m quite sure that the process is very similar to the first release of 2008, and even to 2005. The Excel file that I will be importing is the one used in my previous article, and I’ll also refer to some parts of that article, so you may want to have a read over that one when something here isn’t clear.
Furthermore I’m using a Windows 7 64-bit machine, with the ACE 14 driver (beta) installed. To avoid any discussion about versions and for my own (future) reference, here’s the result of a SELECT @@VERSION:
Microsoft SQL Server 2008 R2 (CTP) – 10.50.1352.12 (X64) Oct 30 2009 18:06:48 Copyright (c) Microsoft Corporation Enterprise Evaluation Edition (64-bit) on Windows NT 6.1 <X64> (Build 7600: )
Create SSIS Package To Import Excel Data
Usually you will start by creating a new package in an Integration Services project, add an Excel source to a new Data Flow, throw in some Data Flow Transformations and end your flow with an OLE DB Destination connecting to your SQL Server.
But that’s not the approach that I’ll take in this article. I’ll make use of a shortcut (and meanwhile I’m showing you how well integrated some components really are).
First, I’m creating a new database called ExcelImport, using the Management Studio (aka SSMS). Once the database is created, right-click on it and choose Tasks > Import Data.
This will open up the SQL Server Import And Export Data Wizard. Like all good wizards, it starts with a Welcome screen containing an introductory text about its purpose – something about “create simple packages that import and export data between many popular data formats including databases, spreadsheets” – and so on. It also includes a checkbox that says “Do not show this starting page again”. That’s my favorite object on the page
Okay, I agree, the page is useful for people who have never seen the wizard before and who may have opened it up by accident, but that’s about as far as its use goes methinks.
So, do whatever you like with the checkbox and click Next. That opens up the Choose a Data Source screen.
In that screen you have several options in the Data Source dropdown. The one we’re interested in is called Microsoft Excel. Once that option is selected, the controls further down the screen change into what is shown in the screenshot. Select your file and the right version, in my case I’ve got an Excel 2007 file. If your sheet contains a header row, activate the First row has column names checkbox.
Clicking Next will open up the Choose a Destination screen.
In that screen, select Microsoft OLE DB Provider for SQL Server as Destination. Ensure that your SQL Server instance is the right one in the Server Name dropdown and the Authentication is filled out as expected. The correct database should be selected by default because we did a right-click on it to start the wizard.
Another Next click opens up the Specify Table Copy or Query window.
Here we can choose between either retrieving the full table – all rows, all columns – or writing a query ourselves. I’ll go for the first option.
Click Next once more to open the Select Source Tables and Views screen. That name seems a bit weird in the context of an Excel import but I guess that’s not really important here. Just read it as “Select the sheet that you’d like to import”.
I’ll go for the ProductList$ Source. The sheet in my Excel file is called ProductList. Note that the Destination is editable – I’m changing the destination table to dbo.ProductList (with the dollar sign removed).
If you’d like to view your data right now you can hit the Preview button. It opens up a window such as this one:
That should look familiar.
A more interesting button is the one called Edit Mappings. Clicking that button opens a screen that lets you change the destination table’s schema. By default, all columns that seem to be numeric are mapped to a type of float and all the others are mapped to nvarchar with a length of 255. Depending on your situation you can either leave it as it is (in case you’re just loading a staging table and want to handle data conversions later on) or you should review each column (for instance when you’re erasing and loading the full table every night and this is the actual table being used by other processes).
Note: whenever a column contains a blank cell, its type will be nvarchar(255), even when all other values are numeric. Also, if you don’t need Unicode support, don’t use nvarchar but change it to varchar instead.
Here’s the Column Mappings screen with some of the defaults changed:
Everything that I changed has been indicated using the yellow marker. I’ve changed some field types and lenghts, made one field non-nullable and adapted the destination name.
Clicking OK followed by Next brings us to the following screen, Review Data Type Mapping.
This screen gives another overview of all the columns that we’re planning to import. Note that each column of which we’ve changed the type has gotten a nice yellow warning sign. This happens because a data conversion needs to occur, and there’s always something that can go wrong when converting data. The On Error and On Truncation columns show the action that should happen when such an event occurs. We’ll leave them all at Use Global. The Global settings are located at the bottom of the screen and are both set to Fail. That’s the best option at the moment (the only other one is Ignore but that means you won’t get any notification in case of an error or truncation problem).
After clicking Next we end up at the Save and Run Package window.
I have activated the Save SSIS Package checkbox and chose the Do not save sensitive data option. By default it is saved in SQL Server, which is actually the MSDB. That’s good because we’re going to examine the contents of the package later on so we want to keep it.
The Run immediately checkbox was activated by default. This will execute the package in the last step of the wizard.
Another Next click and we’re on the Save SSIS Package screen.
Here we can give our package a decent name, such as ExcelImport. You can also see the server on which I’m saving the package.
The final Next click brings us to the Complete the Wizard screen, woohoo!
We can see a short overview of what we’ve configured in the previous steps. Click Finish to execute and save the package!
And we’ve successfully executed the package!
Taking A Closer Look At The SSIS Package
When connecting to the Integration Services server (through SSMS for instance), I now have a new package in the root of the MSDB folder.
It’s located there because I chose to save the package to SQL Server.
Adding An Existing Package To An SSIS Project
We’re now going to open it in the Business Intelligence Development Studio (aka BIDS). So, open the BIDS and create a new SSIS Project (or use an existing one, doesn’t really matter).
Once the project is open, right-click the SSIS Packages folder in the Solution Explorer and select Add Existing Package.
That opens up the Add Copy of Existing Package window.
Sidenote: do you see that SSIS Import and Export Wizard option in the previous screenshot? That’s right, the wizard that we’ve used extensively in the earlier part of this article can be launched from here as well.
Select SSIS Package Store as location of the package and enter the name of your server in the second dropdown. Once that is done you can click the button with the ellipsis and select your package under the MSDB node.
Clicking OK will add the package to your project in the Solution Explorer. Double-click it to open it up.
The Control Flow
In the Control Flow you can see two components: a SQL Task that contains a CREATE TABLE statement and a Data Flow.
Here’s what the CREATE TABLE statement looks like:
As you can see, the table is created using the column names and types just like we configured them through the wizard.
Important to note here is that the Control Flow does not take anything else into account. For instance, what happens if we execute the package twice? It will fail because the table already exists!
In case you don’t believe me, just try it out!
The Data Flow
Opening up the Data Flow we see that it contains three components: an Excel source component that uses an Excel Connection Manager to connect to our now well-known Excel sheet, a Data Conversion Transformation to take care of the conversions that we requested and an OLE DB Destination that uses an OLE DB Connection Manager to connect to our SQL Server.
Important to note here is that whenever an issue occurs, such as a conversion problem, the flow will fail.
In production environments, certain events need to be taken into account. The purpose of this article was just to show you how you can use a wizard to generate an SSIS package for you. You can now use this package as the basis for a well-developed Excel Import template.
Conclusion
With this article I hope to have shown you how to use Integration Services to import Excel data, and also that the Management Studio knows how to use other SQL Server components, such as SSIS, quite well.
If you’re running into some issues while using the wizard, or you just like reading what I write, check out my follow-up article covering some common pitfalls.
Need to go to sleep now, long drive tomorrow, PASS European Conference in Germany! I do hope that Adam Saxton will be there because I was planning to see his presentations all day long. I already read that one of the speakers – Brent Ozar – won’t be able to make it. Darned ash cloud… Next time someone starts talking to me about Azure I’ll run away screaming.
Just kidding
Have fun!
Valentino.
References
Microsoft Support: How to import data from Excel to SQL Server
Tags: data, Excel, Integration Services, SSIS, Tutorial
-
cristina on July 28, 2010 at 8:23 PM
Well, I am newbie
I did alll the step correctly..but I can not access MSDB folder
how do I find that folder on my Windows xp?
regards,
-
Valentino Vranken on July 29, 2010 at 8:08 PM
Hi Cristina,
The “MSDB folder” is not a folder on the OS-level, so it can’t be found on your harddrive. You need to open up the Management Studio and open a connection to the SSIS server. To do that, select “Integration Services” as Server Type in the Connect to Server popup and fill out the other boxes as applicable.
You’ll also need sufficient rights to be able to do that. Have a look here for more info: http://msdn.microsoft.com/en-us/library/aa337083.aspx
Regards,
Valentino.
-
-
Timothy on August 26, 2011 at 2:25 AM
Thank you! These are really good articles. I had no idea that Management Studio could create dtsx packages with a wizard! (Though simple, effective). I do have one serious issue I’m facing though…
The default for a text column is nvarchar(255), which is what mine says when I click on the field in the column mapping section. I changed this to 4000 because this field has long text in it (close to 2000 characters), but this only creates the table with that data type, it doesn’t change the excel SOURCE. I therefore keep getting the error you got, “Text was truncated or one or more characters had no match in the target code page.”
I’ve tried changing it using the advance editor for the excel source data flow in BIDS, but it kicks an error and wont let me (btw, when it gives an error or warning, when you hover over it displays the message, but often the message is so long that it extends passed the little display window and disappears so you cant see it, do you know how to change that??).
Anyways, how do you modify it so that if your excel document has a field with over 255 characters, then you can still import it?? This is driving me nuts!!!
-
Valentino Vranken on September 5, 2011 at 8:49 PM
Hi Timothy,
As you’ve double-posted your comment, please see the other one for my reply (found under http://blog.hoegaerden.be/2010/05/03/importing-data-using-the-wizard-mixing-the-wrong-ingredients)
Regards,
Valentino.
-
-
Don on November 3, 2011 at 5:01 PM
Great article. Very informative. I’ve encountered an issue where all numbers imported from Excel are truncated at 4 decimal places. I’m using TypeGuessRows=0 and ImportMixedTypes=Majority Type in the registry for the ACE 14.0 OLEDB provider but it still truncates at 4. Any help would be appreciated.
Certification



Recent Posts
- Community Day 2012
- SSIS, Flat Files And Accents (é, è, …)
- IT in Transformation: BI and Productivity for Your Business Solutions
- SQL Server Data Tools (SSDT)
- SQLUG Event: The ColumnStore Index
- Building Reports With Dynamic Datasets
- Garmin Edge 705 Auto Pause
- SQL, VS, SPs: Installation Order
- Custom Code in SSIS
- Office Tip: Show All Windows You Silly Thing!
Tags
Categories
Archives
- May 2012 (2)
- April 2012 (3)
- March 2012 (4)
- February 2012 (4)
- January 2012 (2)
- December 2011 (2)
- November 2011 (2)
- October 2011 (1)
- September 2011 (3)
- August 2011 (2)
- June 2011 (2)
- May 2011 (3)
- April 2011 (3)
- March 2011 (3)
- February 2011 (2)
- January 2011 (5)
- December 2010 (1)
- November 2010 (3)
- October 2010 (3)
- September 2010 (2)
- August 2010 (4)
- July 2010 (2)
- June 2010 (4)
- May 2010 (6)
- April 2010 (3)
- March 2010 (3)
- February 2010 (11)
- January 2010 (9)
- December 2009 (2)
- November 2009 (3)
- October 2009 (3)
- September 2009 (4)
- August 2009 (6)
- July 2009 (2)
- June 2009 (3)
- May 2009 (7)
- April 2009 (3)
- March 2009 (3)
- February 2009 (5)
- January 2009 (4)
- December 2008 (2)
- November 2008 (3)
- October 2008 (1)
- September 2008 (1)
- August 2008 (4)
- July 2008 (3)
Recommended Reading
- Just Announced - SQL Server 2012 - coming to you in 2012 October 11, 2011 Dandy Weyn
- How to Install Master Data Services (MDS) Service Pack 1 (for SQL Server 2008 R2) August 16, 2011 mattande
- Expression Adorners August 13, 2011 Matt Masson - MSFT
- Report Authoring on the SSIS Catalog August 1, 2011 Matt Masson - MSFT
- Managing SSIS Projects through SSMS July 20, 2011 mmasson
- Flat File Source Changes in Denali July 17, 2011 mmasson
- Overview of the DQS Cleansing Transform July 14, 2011 mmasson
- SQL Server codename "Denali" CTP3, including Project "Crescent" is now publically available July 12, 2011 Thierry Dhers
- SQL Server Code Name “Denali” CTP3 and SQL Server 2008 R2 SP1 are HERE! July 12, 2011 SQL Server Team
- Fixing SQL Server Management Studio’s Tab Text June 7, 2011 Brent Ozar
Tools You Really Need
Service Packs
SQL Server Material
- Common Solutions for T-SQL Problems
- Microsoft IT Showcase
- Microsoft Learning
- SQL Server 2008 Community Articles
- SQL Server 2008 MCM Readiness Videos
- SQL Server Books Online
- SQL Server Community Projects & Samples
- SQL Server Customer Advisory Team
- SQL Server Homepage
- SQL Server Library
- SQL Server TechCenter




7 comments
Comments feed for this article
Trackback link: http://blog.hoegaerden.be/2010/04/20/importing-excel-data-using-integration-services/trackback/