A while ago I wrote an article about how you can use the SQL Server Import and Export wizard to import Excel data into a SQL Server database. In this sequel I’m going to show you some problems which you may encounter when using that wizard. Just like any good old wizard, he’s only as good as his recipes. If a recipe is missing an ingredient, the resulting potion will probably not behave as expected and before you know it it explodes in your face.
I’ll be using the same Excel file as in my previous article. In case you’re having some problems understanding the data or locating certain screens mentioned in this article, I recommend you to first read the prequel.
Some Common Wizard Pitfalls And Their Solution
Drop And Re-Create Destination Table
Imagine that for your import process you’re planning to import that Excel data regularly. So the logical step is to make sure that each time the process runs, it starts from scratch. Following that, what seems to be an interesting checkbox is located on the Column Mappings screen. This checkbox is called Drop and re-create destination table.
So you activate that checkbox and execute the package. However, it ends with an error in the Executing phase:
Here’s what the error details say:
Error 0xc002f210: Drop table failed… Cannot drop the table dbo.ProductList$ because it does not exist or you do not have permission.
So why does this error occur? Let’s examine the SSIS package that was generated. This is what the Control Flow looks like:
New here is that Drop table SQL Task. Taking a closer look at the task, here’s the query:
drop table [dbo].[ProductList$]
So the first task is to drop the table. This flow does not take into account that the table may not yet exist, as is the case here, causing the error.
However, do you notice the blue arrow connecting the Drop table task with the next SQL Task? That means that it doesn’t require the execution of the task to end successfully, the flow will continue even when an error occurred (unlike when the green connector – meaning Success – is used).
Double-clicking the blue connector gives us the Precedence Constraint Editor window:
Indeed, the value for the constraint is set to Completion.
This also explains why the wizard continued executing all phases, even though an error occurred. And the end result was that the table is really created, containing the data as expected.
If you’d like to avoid the error and handle the non-existing table, you could replace the query in the Drop table SQL Task with the following one:
select * from INFORMATION_SCHEMA.TABLES T
where T.TABLE_NAME = 'ProductList$'
and T.TABLE_SCHEMA = 'dbo'
drop table [dbo].[ProductList$];
It first checks if the table exists, taking schema into account, and will only perform the DROP TABLE statement if the table actually exists.
Field Length Insufficient
On to the next possible issue. When examining our Excel sheet we find out that the content of the Class column is either blank or just one character:
So we decide to change its type to char(1), again using the Column Mappings screen.
However, clicking Finish at the end of the wizard gives us a fatal error:
And these are the details of the error:
Wow, that’s quite a long list for such a simple change! Apparently it wasn’t such a good idea to limit the length of this field. But why does this error occur? After all, the values in this field are just one character long. Except, after taking a closer look at the Excel sheet it turns out that these values contain a trailing space!
One way of avoiding this issue is by specifying a higher length for the field. In this particular case however, you may decide to modify your Data Flow so that it removes the trailing space. Here’s what the Data Flow currently looks like:
The Data Conversion transformation takes care of converting the Class column into a DT_STR of length 1.
We’re going to replace that Data Conversion Transformation with another one, a Derived Column transformation. This is one of the transformations which I’m using all the time.
As you can see in the screenshot, I’ve set it up so that it uses the TRIM() function on the Class input column to remove any leading or trailing spaces. Furthermore I’m using a cast to DT_STR of length 1 to ensure the correct field type. The resulting column is called Class_STR, to clearly indicate that it has been converted to DT_STR.
This is the resulting Data Flow:
Looks like we’ve got an issue with our Destination component now. Which is quite logical: we still need to tell it that it should use the newly-created Class_STR column. So double-click the component.
Double-clicking the Destination component will open up the Restore Invalid Column References Editor. In Available Columns, select the new Class_STR column to replace the Class column which was used previously.
That’s it, your SSIS package will now remove the trailing spaces from the Class column and store it in a column of char(1).
Excel Layout Change
Here’s another common issue when dealing with Excel SSIS imports. If you’re not 100% in control of that Excel sheet, someone will someday make a structural change to it and it will cause your import process to fail.
In this really simple example I’ve opened up the Excel sheet and renamed the Color column to Colour. Which is something that may happen in real life: a British person takes over the maintenance of that product list and sees that the Color column is spelled the wrong way, and corrects it without informing anyone.
What does that mean for our import? Here’s the result when manually executing the package using DTExecUI.exe:
In this case execution will fail because the package cannot find the Color column in the Excel sheet. More precisely it says VS_NEEDSNEWMETADATA. This is a really common error when using Integration Services, but you need to manually update the package to handle such changes.
In other words: try to be in control of that sheet as much as you can, and if possible: set up another way to maintain such data. For instance by using a Master Data Management system. But that’s stuff for later on, in future articles.
As long as you’re aware of some of the common issues into which you may run, I still think the Import and Export Wizard is an interesting option to start your first SSIS package. When running into an issue, I recommend to open up the SSIS package and have a closer look through the Business Intelligence Development Studio. Then make any changes there so you can handle the errors.