Filtering data values in one column based on anoth

2019-07-22 13:26发布

问题:

This is a bit of a conundrum I am trying to solve using SSIS and a conditional-split transformation. I have a .csv file that contains attribute data in one row for each unique user and the values for each attribute in another column. i.e.:

Attribute, Attribute Type

ID, 0000000001

Birthdate, 09/02/1976

Role, Manager

Or something of the sort. I need to split the attributes into columns that include the Attribute Type Data. So the desired outcome would be:

ID,                    Birthdate,              Role,

0000000001,             09/02/1976,            Manager,

I then need to insert them into one SQL table with the new columns.

I was able to accomplish this beautifully with a conditional-split transformation for one column (using the expression Attribute == "ID" for example and then mapping the entire Attribute column in the .csv source onto the ID column in the SQL destination table) but the problem is doing so for the other columns. I can't seem to get a Union All transformation to do what I want it to do.

Any advice?

回答1:

You can achieve that using a script component:

  1. Add a script component
  2. Go to the Inputs and Outputs tab
  3. Add 3 Output columns : ID, BirthDate, Role
  4. Set the SynchronousInput property to None

  1. Inside the script editor, write a similar script:
string ID = "";
string BirthDate = "";
string Role = "";
public override void Input0_ProcessInputRow(Input0Buffer Row)
{
   if(!Row.Attribute_IsNull && !String.IsNullOrWhiteSpace(Row.Attribute))
    {

        switch (Row.Attribute)
        {

            case "ID":
                ID = Row.AttributeType;
                break;

            case "BirthDate":
                BirthDate = Row.AttributeType;
                break;

            case "Role":
                Role = Row.AttributeType;

                Output0Buffer.AddRow();
                Output0Buffer.ID = ID;
                Output0Buffer.Role = Role;
                Output0Buffer.BirthDate = BirthDate;

                break;
            default:
                break;
        }
    }
}
  1. Map the Output columns to the OLE DB Destination