Importing Data Using The Wizard: Mixing The Wrong Ingredients

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.

Column Mappings: 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:

SQL Server Import and Export Wizard: error while Executing

Here’s what the error details say:

image

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:

A Control Flow that doesn't take a non-existing table into account

New here is that Drop table SQL Task.  Taking a closer look at the task, here’s the query:

drop table [dbo].[ProductList$]
GO

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:

Precedence Constraint Editor

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:

if exists
(
    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:

Possible values of the Class field

So we decide to change its type to char(1), again using the Column Mappings screen.

Column Mappings: change type to char(1)

However, clicking Finish at the end of the wizard gives us a fatal error:

SQL Server Import and Export Wizard: Operatoin stopped in Error

And these are the details of the error:

    • Error 0xc02020c5: Data Flow Task 1: Data conversion failed while converting column “Class” (63) to column “Class” (139).  The conversion returned status value 4 and status text “Text was truncated or one or more characters had no match in the target code page.”.(SQL Server Import and Export Wizard)
    • Error 0xc020902a: Data Flow Task 1: The “output column “Class” (139)” failed because truncation occurred, and the truncation row disposition on “output column “Class” (139)” specifies failure on truncation. A truncation error occurred on the specified object of the specified component.

      (SQL Server Import and Export Wizard)

    • Error 0xc0047022: Data Flow Task 1: SSIS Error Code DTS_E_PROCESSINPUTFAILED.  The ProcessInput method on component “Data Conversion 0 – 0″ (131) failed with error code 0xC020902A while processing input “Data Conversion Input” (132). The identified component returned an error from the ProcessInput method. The error is specific to the component, but the error is fatal and will cause the Data Flow task to stop running.  There may be error messages posted before this with more information about the failure.

      (SQL Server Import and Export Wizard)

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:

Data Flow to import Excel data

The Data Conversion transformation takes care of converting the Class column into a DT_STR of length 1.

Data Conversion Transformation Editor

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.

Derived Column Transformation Editor

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:

Data Flow: Destination component complains about lineage ID

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.

 Restore Invalid Column References Editor

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:

DTExecUI: Package Execution Progress

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.

Conclusion

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.

Happy importing!

Valentino.

Share

Tags: , , ,

  1. David Burnham’s avatar

    YES!! It sounds goofy and it drives my wife insane but I get so thrilled when I come across blogs like this. Anything that has the potential to improve my knowledge at a quicker pace than the school classes I’m in are wonderful! Thanks for this great blog! Bookmarked.

    Reply

  2. Timothy’s avatar

    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!!!

    Reply

    1. Valentino Vranken’s avatar

      Hi Timothy,

      Nice catch! I tried it out to see what exactly is going on, and indeed, there seems to be a bug!

      I did exactly the same as you: I changed the length of a nvarchar field to 500 instead of the default 255, using the Column Mappings section. The table was indeed created as specified, but the generated SSIS package does not take it into account!

      But fear not, there’s a solution for everything. What you need to do is change the Excel Source in the Data Flow, but not through the regular Excel Source Editor (which is what you get when you double-click it). You need to right-click and then select Show Advanced Editor. When that one’s open, select the Input and Output Properties tab. On that tab, open up the Excel Source Output > Output Columns nodes. Now you should see the list of fields that you’ve got in your Excel sheet. Select the one that you need to increase and modify it’s Length property.

      That’s it, should work now! I know you wrote that you already tried using the Advanced Editor, but did you do exactly the same as what I described just now? Because it should really work, I tried this out.

      Regards,
      Valentino.

      Reply

      1. Timothy’s avatar

        I ran a test and this did indeed work when I set the length to 4000 (the test Excel document had about 1200 chars)!!!

        I then tried this same setup on the data I was working with previously and it failed. I tried something else. I set the datatype on SQL Server to nvarchar(max), and then went into the Excel Source as above, except instead of choosing Unicode string [DT_WSTR] which has a max length of 4000, I chose Unicode text stream [DT_NTEXT] which then grays out the length field…

        This still failed but with a new error.

        [Excel Source [3633]] Error: Failed to retrieve long data for column “XXXX”.

        So, I’m assuming that I must be breaching the capacity for a field in SQL Server… Would it be possible to just import the first 4000 characters??? How would I do that?

        Reply

© 2008-2012 A Developer's Blog All Rights Reserved