简体   繁体   中英

SSIS load 2 columns of a flat file (first non null only) into a variable

I have a flat file with the following columns

SampleID Rep_Number Product Protein Fat Solids

In the flat file SampleID and Product are populated in the first row only, rest of the rows only have values for Rep_Number, Protein, Fat, Solids. SampleID and Product are blank for the rest of the rows. So my task is to fill those blank rows with the first row that has the sampleID and Product and load into the table.

So task is to pick the first non null SampleID and Product from the flat file and put them in a variable. And rest is all configured. If I can pick the first non null SampleID and Product directly from the flat file and put into their respective variables I can take it from there. That is all I need.

I can connect a script component to flat file source in a data flow task. I need help with the script to pick the first non null values (SampleID and Product),

Need help please. Thanks in advance.

If you are sure you need to store the data of the 1st row - 1st 2 columns' values in variables and take it from there, and DO NOT REQUIRE a change in your original approach, then try this:

  1. You need to have a NEW variable to keep track of the ROW COUNT. Let this be an integer and set it to 0. This will help process only the first row and skip the rest. Let's call this Row_Count
  2. After you retrieve data from the component connected to flat file as the source, connect it to a 'Script Component' and click 'Edit'.
  3. In the 'Script Transformation Editor'click the 'Input Columns' on the left and select the desired columns (say Column_Name1 and Column_Name2) you want to retrieve the value from (ie 1st and 2nd)
  4. Click 'Script' on the left
  5. Under 'Custom Properties', expand the 'ReadWriteVariables'. Add the 2 variables you intend to use for storing the values AND the Row_Count variable.
  6. Click 'Edit Script.
  7. In the editor that opens, double click 'ScriptMain.vb' on the right.
  8. Under the Public Overrides Sub PostExecute() {} procedure type this:

    If Variables.Row_Count = 0 Then

    Variables.Your_Variable1 = Row.Column_Name1

    Variables.Your_Variable2 = Row.Column_Name2

    Variables.Row_Count= Variables.Row_Count + 1

    End If

  9. You have the desired values in your variables, proceed with the rest of your logic.

Note:

  1. If you do not add the variables to the 'ReadWriteVariables', you will not be able to access them in the script.
  2. Based on any other code you might add in the script, you would need to include additional headers if they are not present.

Please mark my post as answer if it helps :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM