Filtering data values in one column based on another column and then inserting values into different columns in same SQL Table

倾然丶 夕夏残阳落幕 提交于 2019-12-11 02:11:14

问题


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


来源:https://stackoverflow.com/questions/56530451/filtering-data-values-in-one-column-based-on-another-column-and-then-inserting-v

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!