Retrieve the column causing an error in SSIS

Problem

When there is an error in the SQL Server Integration Services (SSIS) data flow, you have the ability to redirect the error onto an error path. The system provides you with the error code and the ID of the column causing the error. However, for our logging requirements we need to have the column name as well. How can we achieve this in SSIS?

Solution

In versions of SSIS before SQL Server 2016, there was no easy way in the data flow to retrieve the name of the column that caused a component to fail. It’s easy to retrieve the error description, but for the name of the specific column you only had two options:

The problem is you only get the column lineage ID and there is no easy way to retrieve which column it actually belongs. Luckily this has changed in SSIS 2016 and this tip will explain how you can fetch the column name without too much effort. The solution provided in this tip is valid for SSIS 2016 and later versions.

Test set-up

I used a very easy package, with only one data flow:

Control flow

The data flow has the following structure:

Data flow

The source uses this query to retrieve some sample data:

SELECT Nominator = 10, Denominator = 2
UNION ALL
SELECT Nominator = 20, Denominator = 4
UNION ALL
SELECT Nominator = 30, Denominator = 6
UNION ALL
SELECT Nominator = 40, Denominator = 0
UNION ALL
SELECT Nominator = 50, Denominator = 10;

The Derived Column transformation calculates a multiplication and a division. The division will error out on row 4 because of a division by zero error.

Derived column

The Derived Column is configured to redirect errors to the error path:

Derived column error config

In previous versions of SSIS, you could see the error ID and the column ID when you ran the package:

Data viewer

Note that the data viewer also shows the error description, but this seems to be extra functionality of the data viewer since this column is not part of the error output.

Error output

During debugging, you can find the error column yourself by using the lineage ID of the column – 7 in this example – but for automated logging at the server side it would be useful if this lookup process would be simplified and automated.

Retrieving the error column in SSIS 2016 or later

In the 2016 release of SSIS, a new function has been added to the SSIS library: GetIdentificationStringByLineageID. This function allows us to retrieve the column name by using the lineage ID. To test this, we need to add a script component as a transformation to the data flow.

Add script transformation

In the script component editor, select the ErrorCode and ErrorColumn columns as inputs.

Input columns

Create two new output columns, one for the error description and one for the column name. Make sure the width of the description column is wide enough or the script might return errors. I have chosen 5000 characters as the width in this example.

Output columns

The .NET code is pretty simple: you only need three lines of code to retrieve the description and the column name. These lines should be added in the ProcessInputRow method of your input buffer.

public override void Input0_ProcessInputRow(Input0Buffer Row)
{
    Row.ErrorDescription = this.ComponentMetaData.GetErrorDescription(Row.ErrorCode);
    IDTSComponentMetaData130 componentMetaData = this.ComponentMetaData as IDTSComponentMetaData130;
    Row.ErrorColumnName = componentMetaData.GetIdentificationStringByID(Row.ErrorColumn);
}

When we now run the data flow, we can see the results of the script component:

Success!

Note: when you add a data viewer to the error path in SSIS 2016 (or later), the data viewer will display column name of the error column, just as the error description was shown in earlier versions of SSIS. However, the column is not added to the output and is thus not logged.

Data viewer with extra columns

Conclusion

SSIS 2016 introduces a new function in its API allowing you to easily retrieve the column name of the column that caused an error in the data flow. This improves the logging capabilities of SSIS and it reduces the complexity of existing solutions.

Next Steps

2 Comments

  1. Hi Luther,

    no, I don’t have any experience doing this with custom error handlers. I’m also not a big fan of those, as they “hide” logic from the developer (it’s not always very visible that there are some tasks who do extra work).

    Koen

  2. Koen,
    Have you tried incorporating this into a custom error handler instead of keeping it in the dataflow? If you have, I’d like to see some tips on how to set it up.

Leave a Reply

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