问题
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:
- Add a script component
- Go to the Inputs and Outputs tab
- Add 3 Output columns : ID, BirthDate, Role
- Set the SynchronousInput property to
None
- 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;
}
}
}
- 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